简单的 struts2 action model & form 提交的演示 以及升级到 struts2 2.5

24 篇文章 0 订阅
14 篇文章 0 订阅

简单的 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>

struts2.5 的 web.xml 定义与旧版本不同,2.5的 web.xml 定义如下:

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

struts2.5 的 struts.xml 文件的头也不一样,注意区别:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值