Struts2中action接受参数方法
Struts2中Action接收參數的方法主要有以下三種:
1.使用Action的屬性接收參數:
? ?a.定義:在Action類中定義屬性,創建get和set方法;
? ?b.接收:通過屬性接收參數,如:userName;
? ?c.發送:使用屬性名傳遞參數,如:user1!add?userName=Magci;
2.使用DomainModel接收參數:
? ?a.定義:定義Model類,在Action中定義Model類的對象(不需要new),創建該對象的get和set方法;
? ?b.接收:通過對象的屬性接收參數,如:user.getUserName();
? ?c.發送:使用對象的屬性傳遞參數,如:user2!add?user.userName=MGC;
3.使用ModelDriven接收參數:
? ?a.定義:Action實現ModelDriven泛型接口,定義Model類的對象(必須new),通過getModel方法返回該對象;
? ?b.接收:通過對象的屬性接收參數,如:user.getUserName();
? ?c.發送:直接使用屬性名傳遞參數,如:user2!add?userName=MGC;
實例:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> ?
<web-app version="2.5"
? ?xmlns="http://java.sun.com/xml/ns/javaee"
? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ?
? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?<welcome-file-list> ?
? ?<welcome-file>hello.jsp</welcome-file> ?
?</welcome-file-list> ?
?<filter> ?
? ?<filter-name>struts2</filter-name> ?
? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> ?
?</filter> ?
?<filter-mapping> ?
? ?<filter-name>struts2</filter-name> ?
? ?<url-pattern>/*</url-pattern> ?
?</filter-mapping> ?
</web-app> ?
<?xml version="1.0" encoding="UTF-8"?> ?
<web-app version="2.5"
? ?xmlns="http://java.sun.com/xml/ns/javaee"
? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?<welcome-file-list> ?
? ?<welcome-file>hello.jsp</welcome-file> ?
?</welcome-file-list> ?
?<filter> ?
? ?<filter-name>struts2</filter-name> ?
? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> ?
?</filter> ?
?<filter-mapping> ?
? ?<filter-name>struts2</filter-name> ?
? ?<url-pattern>/*</url-pattern> ?
?</filter-mapping> ?
</web-app> ?
struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/" extends="struts-default">
<action name="user*" class="cn.edu.ahau.mgc.struts2.action.UserAction{1}">
<result>/addSuccess.jsp</result>
</action>
</package>
</struts>
User.java:
package cn.edu.ahau.mgc.struts2.mode; ?
publicclass User { ?
private String userName; ?
private String password; ?
public String getUserName() { ?
returnthis.userName; ?
? ?} ?
publicvoid setUserName(String userName) { ?
this.userName = userName; ?
? ?} ?
public String getPassword() { ?
returnthis.password; ?
? ?} ?
publicvoid setPassword(String password) { ?
this.password = password; ?
? ?} ?
} ?
package cn.edu.ahau.mgc.struts2.mode; ?
publicclass User { ?
private String userName; ?
private String password; ?
public String getUserName() { ?
returnthis.userName; ?
? ?} ?
publicvoid setUserName(String userName) { ?
this.userName = userName; ?
? ?} ?
public String getPassword() { ?
returnthis.password; ?
? ?} ?
publicvoid setPassword(String password) { ?
this.password = password; ?
? ?} ?
} ?
UserAction1.java:
import com.opensymphony.xwork2.ActionSupport;
public class UserAction1 extends ActionSupport {
private String userName;
private String password;
public String add() {
System.out.println("userName: " + userName);
System.out.println("password: " + password);
return SUCCESS;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserAction2.java:
import com.opensymphony.xwork2.ActionSupport;
import cn.edu.ahau.mgc.struts2.mode.User;
public class UserAction2 extends ActionSupport {
private User user;
public String add() {
System.out.println("userName: " + user.getUserName());
System.out.println("password: " + user.getPassword());
return SUCCESS;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
UserAction3.java:
import cn.edu.ahau.mgc.struts2.mode.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction3 extends ActionSupport implements ModelDriven<User> {
private User user = new User();
public String add() {
System.out.println("userName: " + user.getUserName());
System.out.print("password: " + user.getPassword());
return SUCCESS;
}
public User getModel() {
return this.user;
}
}
index.jsp:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Param</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="user1!add?userName=Magci&password=123456">user1!add?userName=Magci&password=123456</a>
<br />
<br />
<a href="user2!add?user.userName=MGC&user.password=abc">user2!add?user.userName=MGC&user.password=abc</a>
<br />
<br />
<a href="user3!add?userName=MaGC&password=000000">user3!add?userName=MaGC&password=000000</a>
</body>
</html>
addSuccess.jsp:
<%@ page language="java"import="java.util.*" pageEncoding="GB18030"%> ?
<% ?
String path = request.getContextPath(); ?
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ?
%> ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ?
<html> ?
?<head> ?
? ?<base href="<%=basePath%>"> ?
? ?<title>AddSuccess</title> ?
? ?<meta http-equiv="pragma" content="no-cache"> ?
? ?<meta http-equiv="cache-control" content="no-cache"> ?
? ?<meta http-equiv="expires" content="0"> ? ? ?
? ?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> ?
? ?<meta http-equiv="description" content="This is my page"> ?
? ?<!-- ?
? ?<link rel="stylesheet" type="text/css" href="styles.css"> ?
? ?--> ?
?</head> ?
?<body> ?
? ?User Add Success! <br> ?
?</body> ?
</html> ?
轉載于:https://blog.51cto.com/lebron/1373552
總結
以上是生活随笔為你收集整理的Struts2中action接受参数方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java】编码规范
- 下一篇: HDOJ 1010 HDU 1010 T