2014-09-19 48 views
2

我有這樣的動作:(e.g不是重定向)如何將結果轉發到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; 
    } 
} 

當它執行我想要調用內部的柱子另一個動作,並傳遞給它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> 

想法完全看到這個動作,但在瀏覽器中我得到404狀態。當我簡單地從瀏覽器調用hello.action時,它就可以工作。重定向也可以。我也嘗試過連鎖,但我的參數沒有通過,它不是很方便。 我在做對吧?如果是這樣,404狀態的原因是什麼?

+0

請參閱此參考http://struts.apache.org/release/2.3.x/docs/redirect-result.html – 2014-09-19 13:19:13

+0

我不想重定向,例如,不發送302狀態的瀏覽器響應。我想調用struts中的另一個動作。 – 2014-09-19 13:24:04

+0

比嘗試這個http://struts.apache.org/release/2.0.x/docs/redirect-action-result.html – 2014-09-19 13:26:18

回答

2

,這可能是有益的: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; 
    } 
} 
+0

謝謝!完美地工作。 – 2016-11-30 10:09:40

+0

很高興聽到我能幫上忙。 – StefanHeimberg 2016-11-30 11:14:30

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> 
+1

您的帖子下方有刪除鏈接。用它。 – 2015-12-17 09:26:55

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> 
+0

'FilterDispatcher'已棄用。沒有struts.xml。這只是整個代碼轉儲而不是真正的答案。 – 2015-12-17 09:35:41

相關問題