Beginners are useful: several ways to upload JSP data to action in STRUTS2

Source: Internet
Author: User

First, give the Struts.xml code:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd "><struts> <package name=" login "namespace="/user "extends=" Struts-default "> < Action name= "*_*" class= "com.wepull.struts2.action. {1} Action "    method=" {2} ">  <result name=" Success ">{1}_{2}.jsp</result>  <result name=" Input ">User_toLogin.jsp</result> </action></package></struts>



The first method of communication:

Import com.opensymphony.xwork2.actionsupport;/** * Mode one: Write the property directly in the action, then write the set, get method * 1. Add attributes to the action, and the property name corresponds to the property name on the JSP *  2. Add the appropriate set method * */public class Useraction extends Actionsupport {private string name;private string Pass;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getpass () {return pass;} public void SetPass (String pass) {This.pass = pass;} Public String Dologin () {string forward= "input";//system.out.println (name+ "" +pass); if ("Wepull". Equalsignorecase (name) && "123". Equalsignorecase (pass)) {  return SUCCESS;   Login successful, return to Success page}else{  return forward;   Login failed, return login}}public String tologin () {return SUCCESS;       Go to login Page}}


--------------------------------------------------------------------------------------------------------------- ---------------

user_tologin.jsp page:

<form action= "User/user_dologin" >    <table>     <tr>      <td> user name </td>      < Td><input type= "text" name= "name" ></td>      <td> </td>     </tr>     <tr>      <td> password </td>      <td><input type= "text" name= "pass" ></td>      <td> </ td>     </tr>     <tr>      <td><input type= "Submit" value= "sure" ></td>      <td><input type= "reset" value= "reset" ></td>      <td> </td>     </tr>    </table>   </form>

##########################################################################

The second way of transmitting the parameter:

Import com.opensymphony.xwork2.actionsupport;/*** mode Two: encapsulate the attribute into a DTO, and pass the value with a DTO name. where properties * 1. Add attributes to the action, and the property name corresponds to the property name * 2 on the JSP. Add the appropriate set method **/public class Useraction extends Actionsupport {private userdto user;public userdto GetUser () {return user;} public void SetUser (userdto user) {this.user = user;} Public String Dologin () {string forward= "input";//system.out.println (User.getname () + "" +user.getpass ());   if ("Wepull". Equalsignorecase (User.getname ()) && "123". Equalsignorecase (User.getpass ())) {return SUCCESS; }else{return forward;}} Public String Tologin () {return SUCCESS;}} --------------------------------------------------------------------------------------------------------------- ---------------Userdto.java code public class Userdto {private string Id;private string Name;private string pass;public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getpass () {RetuRN Pass;} public void SetPass (String pass) {This.pass = Pass;}}


--------------------------------------------------------------------------------------------------------------- ---------------

user_tologin.jsp page:

<form action= "User/user_dologin" >    <table>     <tr>      <td> user name </td>      < Td><input type= "text" name= "User.Name" ></td>      <td> </td>     </tr>     <tr >      <td> password </td>      <td><input type= "text" name= "User.pass" ></td>      <td > </td>     </tr>     <tr>      <td><input type= "Submit" value= "sure" ></td >      <td><input type= "reset" value= "reset" ></td>      <td> </td>     </tr >    </table>   </form>

##########################################################################

The third way of transmitting the parameter:

Import com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.modeldriven;/*** mode three: Realization modeldriven* Implement Modeldriven interface **/public class Useraction extends Actionsupport implements Modeldriven<userdto>{private UserDTO User;public userdto GetUser () {return user;} public void SetUser (userdto user) {this.user = user;} Public String Dologin () {string forward= "input";//system.out.println (User.getname () + "" +user.getpass ()); if ("Wepull". Equalsignorecase (User.getname ()) && "123". Equalsignorecase (User.getpass ())) {  return SUCCESS;   } else{  return forward;}} Public String Tologin () {return SUCCESS;} Public Userdto Getmodel () {if (user==null) {  user=new userdto ();} return user;}}


--------------------------------------------------------------------------------------------------------------- ---------------

user_tologin.jsp page:

<form action= "User/user_dologin" >    <table>     <tr>      <td> user name </td>      < Td><input type= "text" name= "name" ></td>      <td> </td>     </tr>     <tr>      <td> password </td>      <td><input type= "text" name= "pass" ></td>      <td> </ td>     </tr>     <tr>      <td><input type= "Submit" value= "sure" ></td>      <td><input type= "reset" value= "reset" ></td>      <td> </td>     </tr>    </table>   </form>

&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&

In addition, we introduce a method of transmitting the parameter via HttpServletRequest request.

Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpsession;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.modeldriven;/*** how to get the request parameter *httpservletrequest request  method **/public class Useraction extends Actionsupport{public String dologin () {//Get Request Object HttpServletRequest request= Servletactioncontext.getrequest (); Gets the Session object//httpsession session=request.getsession (); String name=request.getparameter ("name"); String pass=request.getparameter ("Pass"); String forward= "Input"; if ("Wepull". Equalsignorecase (name) && "123". Equalsignorecase (pass)) {  return SUCCESS;   } else{  return forward;}} Public String Tologin () {return SUCCESS;}}


In the development of the most common is dtos and implementation of Modeldriven two. To Master Yo! ~


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Beginners are useful: several ways to upload JSP data to action in STRUTS2

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.