Method for accepting parameters for actions in struts2

Source: Internet
Author: User

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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.5"
  3. Xmlns = "http://java.sun.com/xml/ns/javaee"
  4. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
  6. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
  7. <Welcome-file-List>
  8. <Welcome-File> hello. jsp </welcome-File>
  9. </Welcome-file-List>
  10. <Filter>
  11. <Filter-Name> struts2 </filter-Name>
  12. <Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
  13. </Filter>
  14. <Filter-mapping>
  15. <Filter-Name> struts2 </filter-Name>
  16. <URL-pattern>/* </url-pattern>
  17. </Filter-mapping>
  18. </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
  1. Package cn.edu. ahau. MGC. struts2.mode;
  2. Public class user {
  3. Private string username;
  4. Private string password;
  5. Public String GetUserName (){
  6. Return this. Username;
  7. }
  8. Public void setusername (string username ){
  9. This. Username = username;
  10. }
  11. Public String GetPassword (){
  12. Return this. Password;
  13. }
  14. Public void setpassword (string password ){
  15. This. Password = password;
  16. }
  17. }
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
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb18030" %>
  2. <%
  3. String Path = request. getcontextpath ();
  4. String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
  5. %>
  6. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
  7. <HTML>
  8. <Head>
  9. <Base href = "<% = basepath %>">
  10. <Title> addsuccess </title>
  11. <Meta http-equiv = "Pragma" content = "no-Cache">
  12. <Meta http-equiv = "cache-control" content = "no-Cache">
  13. <Meta http-equiv = "expires" content = "0">
  14. <Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
  15. <Meta http-equiv = "Description" content = "this is my page">
  16. <! --
  17. <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
  18. -->
  19. </Head>
  20. <Body>
  21. User add success! <Br>
  22. </Body>
  23. </Html>

From: http://hwy1782.javaeye.com/blog/701904

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.