如何将结果转发到Struts2中的另一个动作?

时间:2014-09-19 13:13:10

标签: java struts2

我有这个动作:

package com.test;

import com.opensymphony.xwork2.Action;

public class TestAction implements Action{
    private String simpleParam;

    public String getSimpleParam() {
        return simpleParam;
    }

    public void setSimpleParam(String simpleParam) {
        this.simpleParam = simpleParam;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

当它被执行时我想在struts中调用另一个动作(例如,不重定向)并传递给它simpleParam。 SecondAction是:

package com.test;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action {
    private String id;
    private String result;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResult() {
        return result;
    }

    @Override
    public String execute() throws Exception {
        result = "result" + getId();
        return SUCCESS;
    }
}

我在struts.xml中看到了一个工作示例,只需键入另一个动作名称和params就可以了。所以我试图这样做:

<struts>
    <package name="main" extends="struts-default">
        <action name="test" class="com.test.TestAction">
            <result name="success">hello.action?id=${simpleParam}</result>
        </action>
        <action name="hello" class="com.test.HelloAction">
            <result>/hello.jsp</result>
        </action>
    </package>
</struts>

Idea完全看到了这个动作,但在浏览器中我获得了404状态。当我只是从浏览器调用hello.action时它就可以工作了。重定向也有效。我也试过连锁店,但是我的参数没有通过,而且不太方便。 我做得对吗?如果是这样,404状态可能是什么原因?

3 个答案:

答案 0 :(得分:4)

这可能会有所帮助:http://struts.apache.org/docs/action-chaining.html

<struts>
    <package name="main" extends="struts-default">
        <action name="test" class="com.test.TestAction">
            <result name="success" type="chain">hello</result>
        </action>
        <action name="hello" class="com.test.HelloAction">
            <result>/hello.jsp</result>
        </action>
    </package>
</struts>

将参数从一个操作传递到另一个操作,您可以使用HttpServletRequest (http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2):

TestAction.java:

package com.test;

import com.opensymphony.xwork2.Action;

public class TestAction implements Action{
    private String simpleParam;

    public String getSimpleParam() {
        return simpleParam;
    }

    public void setSimpleParam(String simpleParam) {
        this.simpleParam = simpleParam;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("id", simpleParam);
        return SUCCESS;
    }
}

HelloAction.java:

package com.test;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action {
    private String id;
    private String result;

    public String getId() {
        return id;
    }

    public String getResult() {
        return result;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        id = (String)request.getAttribute("id");

        result = "result" + getId();
        return SUCCESS;
    }
}

答案 1 :(得分:0)

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
  <display-name>Struts2 Application</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

答案 2 :(得分:0)

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
      <display-name>Struts2 Application</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            </filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
  <display-name>Struts2 Application</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="viewrecords">View Records all</a>


</body>
</html>

edit.jsp文件

<%@ taglib uri="/struts-tags" prefix="s" %>  

<b>id :</b> <s:property value="id"/> <br>
<b>name :</b> <s:property value="name"/> <br>
<b>salary :</b> <s:property value="salary"/> <br>
<s:textfield name="userid" label="phoneno" value="id"/>

UserAction.java

package com.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;



import com.model.User;

import com.userservice.UserService;


public class UserAction  {


    private int id;
    private String name;
    private int salary;
    private int phoneno;

    ArrayList<User> list=new ArrayList<User>();  

    public ArrayList<User> getList() {  
        return list;  
    }  
    public void setList(ArrayList<User> list) {  
        this.list = list;  
    }  
    public String execute(){  
      UserService service= new UserService();
    list=service.getdetails();

     return "success";  
    }  

    public String delete() {
        System.out.println("----------");



        return "success";
    }



    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public int getPhoneno() {
        return phoneno;
    }
    public void setPhoneno(int phoneno) {
        this.phoneno = phoneno;
    }



}

UserService.java

package com.userservice;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.model.User;

public class UserService {
    ArrayList<User> list=new ArrayList<User>();


    public ArrayList<User> getdetails(){
    try{  
          Class.forName("oracle.jdbc.driver.OracleDriver");  
          Connection con=DriverManager.getConnection(  
            "jdbc:oracle:thin:@localhost:1521:xe","oracletest","oracle");  

          PreparedStatement ps=con.prepareStatement("select * from employee");  
          ResultSet rs=ps.executeQuery();  

          while(rs.next()){  
           User user=new User();  
           user.setId(rs.getInt(1));  
           user.setName(rs.getString(2));  
           user.setSalary(rs.getInt(3));  
           user.setPhoneno(rs.getInt(4));  

           list.add(user);  
           System.out.println(rs.getString(2));
          }  

          con.close();  
         }catch(Exception e){e.printStackTrace();}

    return list;

    }

    public ArrayList<User> getList() {  
        return list;  
    }  
    public void setList(ArrayList<User> list) {  
        this.list = list;  
    } 
}

User.java

package com.model;

public class User {

    private int id;
    private String name;
    private int salary;
    private int phoneno;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public int getPhoneno() {
        return phoneno;
    }
    public void setPhoneno(int phoneno) {
        this.phoneno = phoneno;
    }





}

的welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Struts2  Example</title>
</head>
<body>


<table>
<tr>
    <th>id</th>
    <th>Name</th>
    <th>salary</th>
    <th>phoneno</th>

</tr>
<s:iterator value="list" var="user">
    <tr>
        <td><s:property value="id"/> </td>
        <td><s:property value="name"/></td>
        <td><s:property value="salary"/></td>
        <td><s:property value="phoneno"/></td>
        <td><a href="<s:property value="delete"/>">edit</a></td>
        <td><a href="delete?id=<s:property value="id"/>&delete?name=<s:property value="name"/>">edit1</a></td>

    </tr> 
</s:iterator>
</table>
</body>
</html>
</html>