form表單的Action.java檔案和jsp之間資料傳遞

來源:互聯網
上載者:User

標籤:





    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之間資料傳遞

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.