struts2官方 中文教程 系列五:处理表单

先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列五:处理表单  即 http://www.cnblogs.com/linghaoxinpian/p/6906298.html 

下载本章节代码

介绍

在本教程中,我们将探索使用Struts 2来处理表单提交的更多相关内容。我们将讨论如何使用Java model类来存储表单输入,以及如何创建Struts 2表单来匹配这个模型类。

表单和模型类

对于本教程来说,我们需要提供一个用户可以提交的表单以获得一个奖品。我们的业务规则规定,用户必须提供他的姓名、姓氏、电子邮件地址和年龄

/src/model/Person.java

public class Person {
    private String firstName;
    private String lastName;
    private String email;
    private int age;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "First Name: " + getFirstName() + " Last Name:  " + getLastName() + 
        " Email:      " + getEmail() + " Age:      " + getAge() ;
    }
}

注意,每个实例字段都有一个公共的set/get方法。年龄属性是整数类型。

为了收集上述信息,我们将使用Struts 2的form标签。在创建这种表单时,我们需要知道的关键是将每个表单字段绑定到类型Person对象的特定实例字段。让我们先看一下表单,然后再讨论一些关键点。

/WebRoot/register.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Register</title>
  </head>
  <body>
    <h3>Register for a prize by completing this form.</h3>

    <s:form action="register">
      <s:textfield name="personBean.firstName" label="First name" />
      <s:textfield name="personBean.lastName" label="Last name" />
      <s:textfield name="personBean.email"  label ="Email"/>  
      <s:textfield name="personBean.age"  label="Age"  />
      <s:submit/>
    </s:form>    
  </body>
</html>

由于我们使用Struts 2标签,我们需要首先对Struts标签库声明。上述代码中的Struts 2表单将会提交给一个名为register的Action。我们需要在struts.xml中定义这个action节点。在四个Struts2 textfield标签中都有一个name属性,其值是Person类的一个属性(例如firstName)。name属性的值是一个名为personBean的对象的属性的引用。这个对象是Person的实例。

例如name="personBean.firstName",指示Struts2使用该textfield的表单输入值作为personBean对象的setFirstName方法的参数。因此,如果用户在textfield(会被解析成文本框)中输入“大卫”,则personBean的firstName值被自动填充为”大卫“。

注意,对于Person类的每个实例字段,我们都有一个对应的Struts2 textfield标签。请记住Person类的age属性是整数类型。所有表单字段输入值都是字符串。Struts2将会自动地将用户输入的字符串进行相应的类型转换。

新建Action类来处理表单提交

/src/action/Register.java

package action;

import com.opensymphony.xwork2.ActionSupport;

import model.Person;

public class Register extends ActionSupport {

    private Person personBean;

    public String execute() throws Exception {
        // call Service class to store personBean's state in database
        //.....
        return SUCCESS;
    }

    public Person getPersonBean() {
        return personBean;
    }

    public void setPersonBean(Person person) {
        personBean = person;
    }
}

新建注册成功后的结构视图

thankyou.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Registration Successful</title>
  </head>
  <body>
    <h3>Thank you for registering for a prize.</h3>

    <p>Your registration information: <s:property value="personBean" /> </p>

    <p><a href="<s:url action='index' />" >Return to home page</a>.</p>
  </body>
</html>

若是不记得 <s:property value="personBean" /> 和 <s:url action='index' /> 的含义,请花个两分钟时间看一下 struts2 官方系列教程一:使用struts2 标签 

在struts.xml中新建action映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="basicstruts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
        <!--添加如下-->
        <action name="hello" class="action.HelloWorldAction" method="execute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
        
        <!-- register -->
        <action name="register" class="action.Register" method="execute">
            <result name="success">/thankyou.jsp</result>
        </action>
    </package>

</struts>

 在index.jsp中建立一个链接到register.jsp

<p><a href="register.jsp">Please register</a> for our prize drawing.</p>

运行

struts2官方 中文教程 系列五:处理表单

struts2官方 中文教程 系列五:处理表单