码迷,mamicode.com
首页 > 编程语言 > 详细

form表单的Action.java文件和jsp之间数据传递

时间:2015-06-02 17:03:18      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:


技术分享


    demo1

 

      form表单的Action.java文件和jsp之间数据传递
    目标:以注册为模版,描述数据传递过程
1、jsp里的struts2表单(WebContent目录下)
reg.jsp
<s:form name="regForm" action="RegisterAction.action" method="post">
     <s:textfield label="用户名" name="user.userName" title="请输入用户名!"/> 
     <s:textfield label="密码" name="user.passWord" title="请输入密码!"/> 
      <s:textfield label="年龄" name="user.age"/>
     <s:textfield label="Email" name="user.email"/>
     <s:radio label="性别" name="user.sex" list="#{‘nan‘:‘男‘,‘nv‘:‘女‘}"
value="{‘nan‘}" />
     <s:checkboxlist label="爱好" name="user.hobby" list="#{‘0‘:‘篮球‘,‘1‘:‘足
球‘,‘2‘:‘排球‘}" value="{‘0‘}"/>
     <s:select label="班级" name="user.cls" list="{‘12(移动)‘,‘12(技术)‘,‘12(测试)‘}"
value="{‘12(测试)‘}"/>
     <s:textarea label="简介" name="user.remark" cols="20" rows="5" />
     <s:submit value="提交"/>
<!--name属性均是javaBean里的一个属性,这样前台输入的数据才会进入到后台的java代码里-->
2、javaBean详情(src目录下,可以新建包)
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 age) {
  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方法是必不可少的
  return user;
}
public void setUser(User user) {
  this.user = user;
}
public String register(){
   //把注册的信息存入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配置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>
<!--如果注册成功后就到struts2里查找LoginAction1对应的java类,如果注册失败,就提示不合
法的输入-->


5、使用RegisterAction-validation.xml校验输入的信息
<?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>用户名必须输入</message>
            </field-validator>
            <field-validator type="stringlength">
                <param name="minLength">4</param>
                <param name="maxLength">8</param>
                <message>用户名长度访问4-8</message>
            </field-validator>
        </field>
        <field name="user.passWord">
            <field-validator type="requiredstring">
                <message>密码必须输入!</message>
            </field-validator>
            <field-validator type="stringlength">
                <param name="minLength">6</param>
                <message>密码的长度必须大于${minLength}!</message>
            </field-validator>
        </field>
        <field name="user.age">
            <field-validator type="int">
              <param name="min">1</param>
              <param name="max">130</param>
              <message>年龄必须在${min}-${max}之间!</message>
        </field-validator>     
       </field>
   <field name="user.email">
     <field-validator type="email">
        <message>必须输入正确的email地址!</message>
     </field-validator>     
   </field>
     </validators>   
6、运行效果:成功
p1 技术分享
p2

form表单的Action.java文件和jsp之间数据传递

标签:

原文地址:http://www.cnblogs.com/techfox/p/4546429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!