Data passing between the Action.java file of the form form and the JSP

Source: Internet
Author: User





Demo1

data passing between the Action.java file of the form form and the JSPgoal: To register as a template to describe the data transfer process1. Struts2 form in JSP (WebContent directory)
reg.jsp
<s:form name= "RegForm" action= "registeraction.action" method= "POST" >
<s:textfield label= "User name" Name= "user.username" title= "Please enter your username! "/>
<s:textfield label= "Password" name= "User.password" title= "Please enter the password! "/>
<s:textfield label= "Age" name= "User.age"/>
<s:textfield label= "Email" name= "User.email"/>
<s:radio label= "Gender" name= "User.sex" list= "#{' nan ': ' Male ', ' NV ': ' Female '}" value= "{' Nan '}"/>
<s:checkboxlist label= "Hobby" name= "User.hobby" list= "#{' 0 ': ' Basketball ', ' 1 ': ' Foot Ball ', ' 2 ': ' Volleyball '} ' value= ' {' 0 '} '/>
<s:select label= "class" Name= "User.cls" list= "{' 12 (mobile) ', ' 12 (Technology) ', ' 12 (Test) '}" value= "{' 12 (test) '}"/>
<s:textarea label= "Introduction" Name= "User.remark" cols= "5" rows=
<s:submit value= "Submit"/>
<!--The Name property is a property in JavaBean so that data entered in the foreground is entered into the Java code in the background- -2, JavaBean details (src directory, you can create a new package)Package Com.lin.pojo;Public class User {private int UserID;
Private String UserName;
Private String PassWord;
Private String sex;
Private String [] hobby;
private int age;
Private String CLS;
Private String remark;
Private String Email;
public int GetUserID () {
return UserID;
}
public void Setuserid (int userID) {
This.userid = UserID;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String GetPassword () {
return PassWord;
}
public void SetPassword (String passWord) {
This.password = PassWord;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
Public string[] Gethobby () {
return hobby;
}
public void Sethobby (string[] hobby) {
This.hobby = hobby;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String Getcls () {
return CLS;
}
public void Setcls (String cls) {
This.cls = CLS;
}
Public String Getremark () {
return remark;
}
public void Setremark (String remark) {
This.remark = remark;
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}
}3, package com.lin.test;

Import Java.util.Map; import Com.lin.pojo.User;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport; public class Registeraction extends Actionsupport {
User user;

Public User GetUser () {//getset method is essential
return user;
}
public void SetUser (user user) {
This.user = user;
}
Public String Register () {
Depositing registered information in session
SYSTEM.OUT.PRINTLN ("Registeraction Register" +user.getusername ());
SYSTEM.OUT.PRINTLN ("Registeraction Register" +user.getpassword ());
Map<string,object> Map2=actioncontext.getcontext (). GetSession ();
Map2.put ("UserName", User.getusername ());
Map2.put ("PassWord", User.getpassword ());
Return "Success";
}}
4. Struts2 Configure action <action name= "registeraction" class= "com.lin.test.RegisterAction" method= "register" >
<result name= "Success" type= "Chain" >LoginAction1</result>
<result name= "Input" >/register/reg.jsp</result>
</action>
<!--If the registration is successful, you can find the Java class of LoginAction1 in Struts2, if the registration fails, theMethod Input--


5. Using Registeraction-validation.xml to verify the input information
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.2//en"
"HTTP://STRUTS.APACHE.ORG/DTDS/XWORK-VALIDATOR-1.0.2.DTD">
<validators>
<field name= "User.username" >
<field-validator type= "Requiredstring" >
<message> user name must be entered </message>
</field-validator>
<field-validator type= "Stringlength" >
<param name= "MinLength" >4</param>
<param name= "MaxLength" >8</param>
<message> User name length access 4-8</message>
</field-validator>
</field>
<field name= "User.password" >
<field-validator type= "Requiredstring" >
<message> Password must be entered!</message>
</field-validator>
<field-validator type= "Stringlength" >
<param name= "MinLength" >6</param>
The length of the <message> password must be greater than ${minlength}!</message>
</field-validator>
</field>
<field name= "User.age" >
<field-validator type= "int" >
<param name= "min" >1</param>
<param name= "Max" >130</param>
<message> age must be between ${min}-${max}!</message>
</field-validator>
</field>
<field name= "User.email" >
<field-validator type= "Email" >
<message> must enter the correct email address!</message>
</field-validator>
</field>
</validators>
6. Operation Effect: SuccessP1P2

Data passing between the Action.java file of the form form and the JSP

Related Article

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.