form表单的Action.java文件和jsp之间数据传递_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > form表单的Action.java文件和jsp之间数据传递

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

 2015/3/31 0:31:19  麦田的设计者  程序员俱乐部  我要评论(0)
  • 摘要:demo1form表单的Action.java文件和jsp之间数据传递目标:以注册为模版,描述数据传递过程1、jsp里的struts2表单(WebContent目录下)reg.jsp<s:formname="regForm"action="RegisterAction.action"method="post"><s:textfieldlabel="用户名"name="user.userName"title="请输入用户名!"/><s
  • 标签:for 文件 Java 数据 数据传递 表单 JS



?

??? 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

?

  • 大小: 11.8 KB
  • 大小: 15.5 KB
  • 查看图片附件
发表评论
用户名: 匿名