<?xml version="1.0" encoding="GBK"?> <project name="struts" basedir="." default=""> <property name="dist" value="classes"/> <property name="src" value="src"/> <path id="classpath"> <fileset dir="lib"> <include name="*.jar"/> </fileset> <pathelement path="${dist}"/> </path> <target name="compile" description="Compile all source code"> <delete dir="${dist}"/> <mkdir dir="${dist}"/> <copy todir="${dist}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <javac destdir="classes" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <src path="${src}"/> <classpath refid="classpath"/> </javac> </target> </project>
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 定义Struts 2的核心Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 让Struts 2的核心Filter拦截所有请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
# Change the default message type conversion failure XWork. Default .invalid.fieldvalue = {0} field type conversion failures!
#\u6539\u53d8\u9ed8\u8ba4\u7684\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u540e\u7684\u63d0\u793a\u4fe1\u606f xwork.default.invalid.fieldvalue={0}\u5b57\u6bb5\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\uff01
<? xml Version = "1.0" encoding = "GBK"?> <! DOCTYPE Struts the PUBLIC "- // the Apache Software Foundation // DTD Struts the Configuration 2.3 // EN" "http://struts.apache.org/dtds/ 2.3.dtd-Struts "> <Struts> <-! global resources configuration file -> <Constant name =" struts.custom.i18n.resources "value =" Mess "/> < Package Penalty for name =" lee " the extends = "Struts-default"> <Action name = "Login" class = "org.crazyit.app.action.LoginAction"> <-! named input configured logical view, when converted into the logical view of the failure - -> <Result name = "INPUT">/WEB-INF/content/input.jsp</result> <result>/WEB-INF/content/welcome.jsp</result>/WEB-INF/content/input.jsp</result> <result name="error">/WEB-INF/content/welcome.jsp</result> </action> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!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 "> <title> type conversion error handling </ title> <head> </ head> <body> <S: FieldError /> <H3> type conversion error handling </ H3> <P> username <b> input password with a comma (,) spaced </ B> </ P> <S: Action form = "Login"> <S: TextField name = "user" label = "user" /> <TR> <TD colspan = "2"> <S: Submit value = "conversion" Theme = "Simple" /> <S : reset value = "refill" Theme = "Simple" /> </ TD> </ TR> </ S: form> </ body> </ HTML>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!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 "> <title> conversions </ title> <head> </head> <body> <s:actionmessage/> 用户名为:<s:property value="user.name"/><br/> 密码为:<s:property value="user.pass"/><br/> </body> </html>
# Change the default message type conversion failure XWork. Default .invalid.fieldvalue = {0} field type conversion failures!
#\u6539\u53d8\u9ed8\u8ba4\u7684\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u540e\u7684\u63d0\u793a\u4fe1\u606f xwork.default.invalid.fieldvalue={0}\u5b57\u6bb5\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\uff01
<? xml Version = "1.0" encoding = "GBK"?> <! DOCTYPE Struts the PUBLIC "- // the Apache Software Foundation // DTD Struts the Configuration 2.3 // EN" "http://struts.apache.org/dtds/ 2.3.dtd-Struts "> <Struts> <-! global resources configuration file -> <Constant name =" struts.custom.i18n.resources "value =" Mess "/> < Package Penalty for name =" lee " the extends = "Struts-default"> <Action name = "Login" class = "org.crazyit.app.action.LoginAction"> <-! named input configured logical view, when converted into the logical view of the failure - -> <Result name = "INPUT">/WEB-INF/content/input.jsp</result> <result>/WEB-INF/content/welcome.jsp</result>/WEB-INF/content/input.jsp</result> <result name="error">/WEB-INF/content/welcome.jsp</result> </action> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
package org.crazyit.app.action; import com.opensymphony.xwork2.ActionSupport; import org.crazyit.app.domain.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ public class LoginAction extends ActionSupport { private User user; // user的setter和getter方法 public void setUser(User user) { this.user = user; } public User getUser() { return this.user; } public String execute() throws Exception { if (getUser().getName().equals("crazyit.org") && getUser().getPass().equals("leegang") ) { addActionMessage("登录成功!"); return SUCCESS; } addActionMessage("Login failed !!" ); return ERROR; } }
# Specify user attribute class UserConverter required to complete the conversion type
user = org.crazyit.app.converter.UserConverter
package org.crazyit.app.converter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import org.crazyit.app.domain.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ public class UserConverter extends StrutsTypeConverter { Achieve a string type to convert into a composite method of the type public Object ConvertFromString (the Map context , String [] values, Class toClass) { // Create a User instance User User = new new User (); // only the first request for processing parameter array an array element, // and the comma character string into two strings string [] = userValues values [0] .split ( "," ); // for the User assignment example user.setName (userValues [ 0 ]); user.setPass (userValues [ . 1 ]); // returns converted instance of User return User; } // method to convert a string type composite type implemented public// ConvertToString String (the Map context, Object O) { // the value to be converted to a User instance cast User = User (User) O; return "<" + user.getName () + "," + user.getPass ( ) + ">" ; } }
package org.crazyit.app.domain; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ public class User { private String name; private String pass; // name的setter和getter方法 public void setName(String name) { this.name = name; } public String getName() { return this.name; } // pass的setter和getter方法 public void setPass(String pass) { this.pass = pass; } public String getPass() { return this.pass; } }