In struts2, three methods are used to receive parameters for an action:
1. Use the property of action to receive parameters:
A. Definition: Define attributes in the action class and create get and set methods;
B. Receive: receive parameters through properties, such as username;
C. Send: use the property name to pass parameters, for example, user1! ADD? Username = magci;
2. Use domainmodel to receive parameters:
A. Define: Define the model class. In the action, define the object of the model class (New is not required) and create the get and set methods of the object;
B. Receive: parameters are received through object attributes, such as user. GetUserName ();
C. Send: Use the object property to pass parameters, such as user2! ADD? User. Username = MGC;
3. Use modeldriven to receive parameters:
A. Definition: Action implements the modeldriven generic interface, defines the object of the model class (must be new), and returns the object through the GetModel method;
B. Receive: parameters are received through object attributes, such as user. GetUserName ();
C. Send: directly use the property name to pass the parameter, for example, user2! ADD? Username = MGC;
Instance:
Web. xml:
Java code
- <? 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:
Write <? XML version = "1.0" encoding = "UTF-8"?>
<! 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:
Java code
- Package cn.edu. ahau. MGC. struts2.mode;
- Public class user {
- Private string username;
- Private string password;
- 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;
- }
- }
package cn.edu.ahau.mgc.struts2.mode;public class User { private String userName; private String password; 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; }}
Useraction1.java:
Write package cn.edu. ahau. MGC. struts2.action;
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:
Write package cn.edu. ahau. MGC. struts2.action;
Import com. opensymphony. xwork2.actionsupport;
Import cn.edu. ahau. MGC. struts2.mode. user;
Public class useraction2 extends actionsupport {
Private 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:
Write package cn.edu. ahau. MGC. struts2.action;
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 = 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:
Wrote <% @ 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> 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:
Java code
- <% @ 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>
From: http://hwy1782.javaeye.com/blog/701904