项目结构以及相应jar包见下图:
1、配置web.xml文件
<?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>Struts2-test-2-zh</display-name>
<filter>
<filter-name>Struts2-test-2-zh</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2-test-2-zh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、新建jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="/struts-dojo-tags" prefix="sd"%>
<!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>用户注册</title>
</head>
<s:head theme="xhtml"/>
<sd:head parseContent="true"/>
<body>
<s:form action="register.action" method="post">
<s:textfield name="name" label="用户名称"></s:textfield>
<s:textfield name="age" label="用户年龄"></s:textfield>
<sd:datetimepicker name="birthday" label="出生日期"></sd:datetimepicker>
<sd:textarea cols="20" rows="10" name="intro" label="用户介绍"></sd:textarea>
<s:submit value="注册"></s:submit>
</s:form>
</body>
</html>
3、新建action
/*
*@Author swxctx
*@time 2016年9月29日
*/
package com.sw.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String name;
private int age;
private Date birthday;
private String intro;
public RegisterAction() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(name+","+age+","+birthday+","+intro);
return "success";
}
}
4、struts.xml配置文件
<?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>
<package name="Strute2-test-1" extends="struts-default">
<action name="register" class="com.sw.action.RegisterAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
5、结果页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="/struts-dojo-tags" prefix="sd" %>
<!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>注册成功-用户信息</title>
</head>
<body>
用户名称:<s:property value="name"/><br/>
用户年龄:<s:property value="age"/><br/>
出生日期:<s:date name="birthday" format="yyyy-MM-dd" nice="true"/><br/>
用户介绍:<s:property value="intro"/>
</body>
</html>