Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts 2. The 2.x framework is the best choice for teams who value elegant solutions to difficult problems.
1.struts-2.0.11.1
2.
- Servlet API 2.4
- JSP API 2.0
- Java 5
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<!--<constant name="struts.custom.i18n.resources" value="messageResource"></constant>-->
<package name="com.xmddl.struts.logon" extends="struts-default" namespace="/example">
<interceptors>
<interceptor-stack name="helloWorldStack">
<interceptor-ref name="paramsPrepareParamsStack"/>
</interceptor-stack>
</interceptors>
<!-- 全局results配置 -->
<global-results>
<result name="error">/error.jsp</result>
<result name="input">/index.jsp</result>
</global-results>
<action name="CommonAction"
class="com.xmddl.common.CommonAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
<action name="LogonAction" class="com.xmddl.struts.logon.LogonAction">
<result>/index.jsp</result>
</action>
</package>
<package name="com.xmddl.struts.business" extends="struts-default"
namespace="/example">
<action name="UserInfo"
class="com.xmddl.struts.business.UserInfoAction">
<result name="success" type="dispatcher">/helloworld.jsp</result>
<param name="url">http://www.sina.com</param>
</action>
</package>
</struts>
5.action
package com.xmddl.struts.logon;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.xmddl.common.CommonAction;
import com.xmddl.common.MockDB;
import com.xmddl.common.XmddlException;
import com.xmddl.struts.vo.UserInfoVO;
/**
*
* @author Administrator
*
*/
public class LogonAction extends CommonAction {
/**
*
*/
private static final long serialVersionUID = 1L;
private UserInfoVO uservo = new UserInfoVO();
public String execute() throws Exception {
System.out.println(uservo.getUser().getUsername() + "\t"
+ uservo.getUser().getPassword());
return SUCCESS;
}
/**
* 功能:增加
*
* @return
*/
@SkipValidation
public String saveUserInfo() {
// try {
// if (uservo.getUser().getUsername().equals("xmddl369"))
// throw new XmddlException("Hello World!");
// } catch (Exception ex) {
// this.addActionMessage(ex.getMessage());
// return ERROR;
// }
MockDB.insert2DB(uservo.getUser());
uservo.getUserList().addAll(MockDB.searchAll2DB());
return SUCCESS;
}
/**
* 功能:查询
*
* @return
*/
@SkipValidation
public String searchUserInfo() {
if(uservo.getUser().getUserid()!=null)
{
uservo.getUserList().add(MockDB.searchFromDB(uservo.getUser().getUserid()));
uservo.setUser(MockDB.searchFromDB(uservo.getUser().getUserid()));
}else
{
uservo.getUserList().addAll(MockDB.searchAll2DB());
uservo.setUser(null);
}
return SUCCESS;
}
/**
* 功能:删除
*
* @return
*/
@SkipValidation
public String deleteUserInfo() {
MockDB.deleteFromDB(uservo.getUser());
uservo.getUserList().addAll(MockDB.searchAll2DB());
return SUCCESS;
}
/**
* 功能:更新
*
* @return
*/
@SkipValidation
public String updateUserInfo() {
MockDB.update2DB(uservo.getUser());
uservo.getUserList().addAll(MockDB.searchAll2DB());
return SUCCESS;
}
public UserInfoVO getUservo() {
return uservo;
}
public void setUservo(UserInfoVO uservo) {
this.uservo = uservo;
}
}
6.xxx-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="uservo.user.userid">
<field-validator type="requiredstring">
<message key="userid.required"/>
</field-validator>
</field>
<field name="uservo.user.username">
<field-validator type="requiredstring">
<message key="username.required"/>
</field-validator>
</field>
<field name="uservo.user.password">
<field-validator type="requiredstring">
<message key="password.required"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">15</param>
<message key="password.length"/>
</field-validator>
</field>
</validators>
7.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div style="text-align: center; font-size: 14px; font-weight: bold">
This is my struts2 JSP test page.
</div>
<br>
<div style="text-align: right;">
<s:include value="/LanguageSelector.jsp" />
</div>
<!--
<s:text name="loginPage" />
<s:property value="getText('loginPage')" />
<s:textfield key="loginPage"/>
<s:label value="%{#SESSION_LOCALE}" />
-->
<s:form action="LogonAction" name="main" namespace="/example" cssStyle="text-align:center">
<!--
<s:bean name="com.xmddl.struts.vo.UserInfo" id="ui">
<s:param name="sex" value="1"/>
</s:bean>
<s:bean name="com.xmddl.struts.vo.UserInfoVO" id="uivo">
<s:param name="user" value="ui"/>
</s:bean>
<s:property value="#uivo.user.sex"/> -->
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<thead>
<tr>
<td colspan="2"
style="text-align: center; font-size: 14px; font-weight: bold">
CRUD Test
</td>
</tr>
<tr>
<td>
<s:textfield label="%{getText('userId')}" name="uservo.user.userid"
value="%{uservo.user.userid}" />
<s:textfield label="%{getText('userName')}" name="uservo.user.username"
value="%{uservo.user.username}" />
<s:textfield label="%{getText('userAge')}" name="uservo.user.age"
value="%{uservo.user.age}" />
<s:radio list="#{1:'男',0:'女'}" name="uservo.user.sex" label="%{getText('userSex')}" />
<s:select list="#{1:'厦门',2:'福州',3:'宁德'}" name="uservo.user.from" label="%{getText('userFrom')}"></s:select>
<s:password label="%{getText('userPass')}" name="uservo.user.password"
value="%{uservo.user.password}" cssStyle="display:none"/>
</td>
</tr>
</thead>
</table>
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<tbody>
<tr
class="<s:if test="#status.even">even</s:if><s:else>odd</s:else>">
<td>
<s:property value="getText('userId')" />
</td>
<td>
<s:property value="getText('userName')" />
</td>
<td>
<s:property value="getText('userSex')" />
</td>
<td>
<s:property value="getText('userAge')" />
</td>
<td>
<s:property value="getText('userFrom')" />
</td>
<td>
<s:property value="getText('update')" />
</td>
<td>
<s:property value="getText('delete')" />
</td>
</tr>
<s:iterator value="uservo.userList" status="status">
<tr>
<td>
<s:property value="userid" />
</td>
<td>
<s:property value="username" />
</td>
<td>
<s:if test="sex==1">男</s:if>
<s:else>女</s:else>
</td>
<td>
<s:property value="age" />
</td>
<td>
<s:if test="from==1">厦门</s:if>
<s:if test="from==2">福州</s:if>
<s:if test="from==3">宁德</s:if>
</td>
<td>
<a
href="">修改</a>
</td>
<td>
<a
href="/Struts2Demo/example/LogonAction!deleteUserInfo.action?uservo.user.userid=<s:property value="userid" />&user.username=<s:property value="username" />">删除</a>
</td>
</tr>
</s:iterator>
</tbody>
<tfoot>
<tr>
<td style="text-align:right" colspan="7">
<s:submit value="%{getText('login')}" method="execute"
theme="simple" cssStyle="display:none"/>
<s:submit value="%{getText('save')}" method="saveUserInfo"
theme="simple" />
<s:submit value="%{getText('update')}" method="updateUserInfo"
theme="simple" />
<s:submit value="%{getText('search')}" method="searchUserInfo"
theme="simple" />
<s:reset value="%{getText('reset')}" theme="simple" />
</td>
</tr>
</tfoot>
</table>
</s:form>
</body>
</html>
8.look like: