从 Struts2 Action 类调用 Javascript

发布于 2024-10-12 23:23:54 字数 2073 浏览 0 评论 0原文

我想从struts2调用一个javascript函数。

package com.example.controller;

import java.io.IOException;
import java.io.PrintWriter;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;


public class FileUploadAction extends ActionSupport implements ServletResponseAware {

 private static final long serialVersionUID = 1L;
 HttpServletResponse response;

 public String execute() throws IOException{

  System.out.println("I am in Action");

  PrintWriter out;
  out = response.getWriter();


  response.setContentType("text/html");  
  out.println("<html>");
  out.println("<head>");
  out.println("<script type=\"text/javascript\">");
  out.println("function foo() { ");
  out.println("alert('From Struts Action');");
  out.println("window.top.uploadComplete('1');");
  out.println("}");
  out.println("</script>");
  out.println("</head>");
  out.println("<body onload=\"foo();\">");
  out.println("</body>");
  out.println("</html>");

  return SUCCESS;
 }

 @Override
 public void setServletResponse(HttpServletResponse response) {
  this.response = response;
 }
 public HttpServletResponse getServletResponse() {
  return this.response;
 }

}

在这里,我什至没有得到“警觉”。即它不起作用, 在后端它会抛出警告

    [WARN] 404 - GET /success (127.0.0.1) 1393 bytes
   Request headers
      Host: 127.0.0.1:8888
      User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: en-gb,en;q=0.5
      Accept-Encoding: gzip,deflate
      Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
      Keep-Alive: 115
      Connection: keep-alive
      Cookie: JSESSIONID=pvv1gasa4o7l
      Referer: http://127.0.0.1:8888/GWTDemo.html?gwt.codesvr=127.0.0.1:9997
   Response headers
      Content-Type: text/html; charset=iso-8859-1
      Content-Length: 1393

请帮忙?

I want to call a javascript function from struts2.

package com.example.controller;

import java.io.IOException;
import java.io.PrintWriter;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;


public class FileUploadAction extends ActionSupport implements ServletResponseAware {

 private static final long serialVersionUID = 1L;
 HttpServletResponse response;

 public String execute() throws IOException{

  System.out.println("I am in Action");

  PrintWriter out;
  out = response.getWriter();


  response.setContentType("text/html");  
  out.println("<html>");
  out.println("<head>");
  out.println("<script type=\"text/javascript\">");
  out.println("function foo() { ");
  out.println("alert('From Struts Action');");
  out.println("window.top.uploadComplete('1');");
  out.println("}");
  out.println("</script>");
  out.println("</head>");
  out.println("<body onload=\"foo();\">");
  out.println("</body>");
  out.println("</html>");

  return SUCCESS;
 }

 @Override
 public void setServletResponse(HttpServletResponse response) {
  this.response = response;
 }
 public HttpServletResponse getServletResponse() {
  return this.response;
 }

}

Here, I am not getting even 'alert'. i.e. it is not working,
and In back end it throws warning

    [WARN] 404 - GET /success (127.0.0.1) 1393 bytes
   Request headers
      Host: 127.0.0.1:8888
      User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: en-gb,en;q=0.5
      Accept-Encoding: gzip,deflate
      Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
      Keep-Alive: 115
      Connection: keep-alive
      Cookie: JSESSIONID=pvv1gasa4o7l
      Referer: http://127.0.0.1:8888/GWTDemo.html?gwt.codesvr=127.0.0.1:9997
   Response headers
      Content-Type: text/html; charset=iso-8859-1
      Content-Length: 1393

Please help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-10-19 23:23:54

您尝试提交两个响应——首先直接提交到 HttpServletResponse,然后使用 Struts2 命名结果(即 SUCCESS)。

将 JavaScript 代码放置在视图层中。

You are trying to commit two responses -- first directly to the HttpServletResponse and then using the Struts2 named result (i.e., SUCCESS).

Place the JavaScript code in your view layer.

不打扰别人 2024-10-19 23:23:54

您能描述一下为什么您需要从 Struts2 操作调用 javascript 吗?这可能会帮助我们为您提供更好的方法,史蒂文所说的是正确的,您尝试提交 2 个响应

Can you describe why you need to call javascript from Struts2 action may be it will help us to provide you better way and what Steven said is correct you tried commiting 2 responses

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文