struts 2 1つのAction内に複数のリクエスト処理方法を含む処理と複数のactionリクエスト


Struts1   DispatchAction,      Action           。Struts2         。             : 

1.1.       : 

DMI:Dynamic Method Invocation       。 

        :     action       Action   ,                : 

<form method="post" action="userOpt!login.action"> 

            ”userOpt” Action  ,Action       ”login”       。  login        execute()  ,  public String login() throws Exception。 

  :         ,    Struts2        ,    struts.enable.DynamicMethodInvocation     ,          true。 

1.1.1.      : 

          ,           。 

1.      Action : 

Java   
1.package org.qiujy.web.struts2.action;   
2.import com.opensymphony.xwork2.ActionContext;   
3.import com.opensymphony.xwork2.ActionSupport;   
4.  
5./**  
6.*@authorqiujy  
7.*@version1.0  
8.*/  
9.  
10.publicclass LoginAction extends ActionSupport{   
11.private String userName;   
12.private String password;   
13.private String msg; //         
14./**  
15.    *@returnthemsg  
16.    */  
17.  
18.public String getMsg() {   
19.       returnmsg;   
20.}   
21.  
22./**  
23.    *@parammsgthemsgtoset  
24.    */  
25.  
26.publicvoid setMsg(String msg) {   
27.       this.msg = msg;   
28.}   
29.  
30./**  
31.    *@returntheuserName  
32.    */  
33.  
34.public String getUserName() {   
35.       returnuserName;   
36.}   
37.  
38./**  
39.    *@paramuserNametheuserNametoset  
40.    */  
41.  
42.publicvoid setUserName(String userName) {   
43.       this.userName = userName;   
44.}   
45.  
46./**  
47.    *@returnthepassword  
48.    */  
49.  
50.public String getPassword() {   
51.       returnpassword;   
52.}   
53.  
54./**  
55.    *@parampasswordthepasswordtoset  
56.    */  
57.  
58.publicvoid setPassword(String password) {   
59.       this.password = password;   
60.}   
61.  
62./**  
63.    *       login()    
64.    *@return         
65.    *@throwsException  
66.    */  
67.  
68.public String login() throws Exception{   
69.       if("test".equals(this.userName) && "test".equals(this.password)){   
70.         msg = "    ,  " + this.userName;   
71.         //  ActionContext  ,      Servlet API   
72.         ActionContext context = ActionContext.getContext();   
73.         // session           ,     :       ;   
74.//               
75.         if(null != context.getSession().get("uName")){   
76.            msg = this.userName + ":       !!!";   
77.         }else{   
78.            context.getSession().put("uName", this.userName);   
79.         }           
80.         returnthis.SUCCESS;   
81.       }else{   
82.         msg = "    ,       ";   
83.         returnthis.ERROR;   
84.       }   
85.}   
86.  
87.public String regist() throws Exception{   
88.       //    ,            
89.       //...   
90.       msg = "    。";   
91.       returnthis.SUCCESS;   
92.}   
93.  
94.}  
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
*@authorqiujy
*@version1.0
*/

publicclass LoginAction extends ActionSupport{
private String userName;
private String password;
private String msg; //      
/**
    *@returnthemsg
    */

public String getMsg() {
       returnmsg;
}

/**
    *@parammsgthemsgtoset
    */

publicvoid setMsg(String msg) {
       this.msg = msg;
}

/**
    *@returntheuserName
    */

public String getUserName() {
       returnuserName;
}

/**
    *@paramuserNametheuserNametoset
    */

publicvoid setUserName(String userName) {
       this.userName = userName;
}

/**
    *@returnthepassword
    */

public String getPassword() {
       returnpassword;
}

/**
    *@parampasswordthepasswordtoset
    */

publicvoid setPassword(String password) {
       this.password = password;
}

/**
    *       login()  
    *@return       
    *@throwsException
    */

public String login() throws Exception{
       if("test".equals(this.userName) && "test".equals(this.password)){
         msg = "    ,  " + this.userName;
         //  ActionContext  ,      Servlet API
         ActionContext context = ActionContext.getContext();
         // session           ,     :       ;
//            
         if(null != context.getSession().get("uName")){
            msg = this.userName + ":       !!!";
         }else{
            context.getSession().put("uName", this.userName);
         }        
         returnthis.SUCCESS;
       }else{
         msg = "    ,       ";
         returnthis.ERROR;
       }
}

public String regist() throws Exception{
       //    ,         
       //...
       msg = "    。";
       returnthis.SUCCESS;
}

}

2.    struts.xml  :      ,        

<!DOCTYPE struts PUBLIC 
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
       "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
<package name="my" extends="struts-default" namespace="/manage"> 
<!--       URL login.action Action --> 
       <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction"> 
       <!--                     --> 
         <result name="success">/success.jsp</result> 
         <result name="error">/error.jsp</result> 
       </action> 
</package> 
</struts> 

3.      : 

index.jsp 

<%@ page language="java" pageEncoding="UTF-8"%> 
<html> 
<head> 
<title>      </title> 
</head> 
<body> 
   <h2>    </h2> 
   <hr> 
<form action="manage/userOpt!login.action" method="post"> 
<table border="1"> 
      <tr> 
         <td>   :</td> 
         <td><input type="text" name="userName"/></td> 
      </tr> 
      <tr> 
         <td>  :</td> 
         <td><input type="password" name="password"/></td> 
      </tr> 
      <tr> 
         <td colspan="2"> 
                <input type="submit" value="    "/> 
         </td> 
      </tr> 
</table> 
</form> 
</body> 
</html> 

regist.jsp 

<%@ page language="java" pageEncoding="UTF-8"%> 
<html> 
<head> 
<title>      </title> 
</head> 
<body> 
   <h2>    </h2> 
   <hr> 
<form action="manage/userOpt!regist.action" method="post"> 
<table border="1"> 
      <tr> 
         <td>   :</td> 
         <td><input type="text" name="userName"/></td> 
      </tr> 
      <tr> 
         <td>  :</td> 
         <td><input type="password" name="password"/></td> 
      </tr> 
      <tr> 
         <td colspan="2"> 
                <input type="submit" value="    "/> 
         </td> 
      </tr> 
</table> 
</form> 
</body> 
</html> 

1.2.  Action  method  : 

 Action                  Action  。 

<!DOCTYPE struts PUBLIC 
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
       "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
<package name="my" extends="struts-default" namespace="/manage"> 
       <action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login"> 
         <result name="success">/success.jsp</result> 
         <result name="error">/error.jsp</result> 
       </action>      

       <action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist"> 
         <result name="success">/success.jsp</result> 
         <result name="error">/error.jsp</result> 
       </action> 
</package> 

</struts> 

  , LoginAction  login regist        Action。   login  ,     index.jsp      action   "manage/userLogin.action";   regist  , regist.jsp      action   "manage/userRegist.action"。 

1.3.        (wildcard mappings)  : 

 struts.xml     <action…>   ,  name、class、method         ,                     。 

          Action name   ,        action       Action: 

<action name="user_*" 
class="org.qiujy.web.struts2.action.UserAction" method="{1}"> 
         <result name="success">/success.jsp</result> 
         <result name="error">/error.jsp</result> 
       </action> 

  ,<action name=”user_*”>       URL user_*.action     Action。  method         {1},      name       *  。  :    URL user_login.action ,    UserAction  login  ;    URL user_regist.action ,    UserAction  regist  。 

------------------------------------------
  action  ,strus_xml  :
[color=blue][align=center]
<action name="Login" class="LoginAction">
    <result name="View">/WEB-INF/jsp/system/login/login.jsp</result>
     <result name="Login" type="redirect-action">Login!welcome</result>
     <result name="Logout" type="redirect-action">Login!showLogin?error=${error}&amp;username=${username}</result>
      <result name="Welcome">/WEB-INF/jsp/system/login/welcome.jsp</result>
      <interceptor-ref name="defaultStack"></interceptor-ref>
	</action>[/b][/color][/[/align]code]
  :http://wallimn.iteye.com/blog/693158