Struct2(五)处理表单

简介:

1.表单的提交

表单和对应的Java模型的类

在此次的例子中,我们将会模仿一个用户提交表单的动作,具体提交表单做什么,不关心,我们需要知道 first last Name,Email address ,age。

为了封装这个数据,我们提供一个简单的java Class 来存储这个信息。

Person.java

package org.apache.struts.helloworld.model;

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() ;

    }

}
表单的展示:
      register.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>



<%@ taglib prefix="s" uri="/struts-tags" %>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

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

我们使用了标签,则需要添加taglib 命令

表单中定义了Action register,我们需要在structs.xml文件中生命。

<action name="register" class="org.apache.struts.register.action.Register" method="execute">
      <result name="success">/ThankYou.jsp</result>
    </action>

 

增加注册动作对应的Action

package org.apache.struts.register.action;



import org.apache.struts.model.Person;



import com.opensymphony.xwork2.ActionSupport;



public class Register extends ActionSupport {



    private static final long serialVersionUID = 1L;



    private Person personBean;



    @Override

    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页面:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

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

在index.jsp中添加:

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

能够定位到register.jsp页面。

测试:

1. index.action

image

2. 点击注册

image

3.提交

image

你可能感兴趣的:(struct)