Struts 版本号 struts-2.3.16.3
web.xml 配置
<?xml version=”1.0″ encoding=”UTF-8″?> <web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.custom.i18n.resources</param-name> <param-value>mess</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
structs.xml 配置
<?xml version=”1.0″ encoding=”UTF-8″ ?> <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.3//EN” “http://struts.apache.org/dtds/struts-2.3.dtd”> <struts> <constant name=”struts.custom.i18n.resources” value=”mess”></constant> <constant name=”struts.i18n.encoding” value=”UTF-8″></constant> <package name=”ge” namespace=”/” extends=”struts-default”> <action name=”login” class=”ge.LoginAction”> <result name=”input”>/login.jsp</result> <result name=”error”>/error.jsp</result> <result name=”success”>/welcome.jsp</result> </action> </package> </struts>
mess.properties 配置
loginPage=登陆页面 errorpage=错误页面 succPage=成功页面 failTip=对不起,您不能登陆 succTip=欢迎,{0},您已登陆 user=用户名 pass=密码 login=登陆
LoginAction 类
package ge; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String username; private String password; public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } public String getPassword(){ return password; } public void setPassword(String password){ this.password=password; } public String execute () throws Exception{ if(getUsername().equals(“ge”) && getPassword().equals(“1″)){ ActionContext.getContext().getSession().put(“user”, getUsername()); return SUCCESS; }else{ return ERROR; } } }
welcome.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><s:text name=”succPage”></s:text></title> </head> <body> 登陆成功 </body> </html>
login.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><s:text name=”loginPage”/></title> </head> <body> <s:form action=”login” method=”post” namespace=”/”> <s:textfield name=”username” key=”user”/> <s:textfield name=”password” key=”pass”/> <s:submit key=”login”/> </s:form> </body> </html>
error.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><s:text name=”errorpage”></s:text></title> </head> <body> 登陆失败了,哈哈哈 </body> </html>