您的位置:首页 > 编程语言 > Java开发

简单的 struts2 action model & form 提交的演示

2016-02-23 01:26 423 查看
简单的 struts2 action model & form 提交的演示,继承 ActionSupport,由 struts.xml 定义 action 指向的该类,用于接受来自 from 的提交。

相关或需要的文件如下:

web.xml 需要增加 struts2 的过滤器定义

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>sfmisStruts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>


src 根目录 struts.xml 文件,定义 action

分别定义了 3 个 action,index、register_input 和 register,分别是欢迎界面,录入界面和显示界面。
action name="动作名" class="处理类" method="类方法"

xml 中的这段 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> 不能遗漏,否则不正常。

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

<struts>
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.locale" value="zh_cn" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />

<package name="basicstruts2" extends="struts-default" namespace="/pathA">

<!-- If no class attribute is specified the framework will assume success and render the result index.jsp -->
<!-- If no name value for the result node is specified the success value is the default -->
<action name="index">
<result>/index.jsp</result>
</action>

<action name="register_input" class="struts.helloworld.action.Register" method="input">
<result name="input">/register.jsp</result>
</action>

<!-- for logger interceptor see: http://struts.apache.org/2.2.1/docs/logger-interceptor.html -->
<!-- for timer interceptor see http://struts.apache.org/2.2.1/docs/timer-interceptor.html -->
<action name="register" class="struts.helloworld.action.Register" method="execute">
<interceptor-ref name="timer" />
<interceptor-ref name="logger" />
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<result name="success">/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>

</struts>


struts action: Register.java

继承 ActionSupport,setPersonBean 方法接收 form 提交,这个方法由 struts.xml 定义。

package struts.helloworld.action;

import java.text.DateFormat;
import java.util.Date;
import struts.helloworld.model.Person;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport
{

private static final long serialVersionUID = 1L;
private Person personBean;

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

public void validate()
{

if (personBean.getFirstName().length() == 0)
{
addFieldError("personBean.firstName", "First name is required.");
}

if (personBean.getEmail().length() == 0)
{
addFieldError("personBean.email", "Email is required.");
}

if (personBean.getAge() < 18)
{
addFieldError("personBean.age", "Age is required and must be 18 or older");
}
}

public Person getPersonBean()
{
return personBean;
}

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

}


strust model: Person.java

form 对象,也可以说是数据对象,对应数据表的字段或者对应 from 提交的 field 信息。

package struts.helloworld.model;

/**
* Models a Person who registers.
*
* @author bruce phillips
*
*/
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();
}
}


web root 目录:index.jsp

欢迎页,显示进入 register 的注册页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p>
<a href="<s:url action='register_input' namespace="/pathA" />">Please register</a> for our prize drawing.
</p>
</body>
</html>


web root / 目录: register.jsp

对应 Register.java 内的 private Person 定义的 personBean。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
<s:head />
</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>


web root / 目录: thankyou.jsp

显示结果。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>


Eclipse 项目目录结构如下:



参考资料:http://struts.apache.org/

Q群讨论:236201801
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: