目录结构:
二、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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2InAction</display-name> <welcome-file-list> <welcome-file>index.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>
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.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="struts2InAction.LoginAction" method="login"> <result name="success">/WEB-INF/view/success.jsp</result> <result name="fail">/WEB-INF/view/fail.jsp</result> </action> <action name="toLogin" class="struts2InAction.ToLoginAction" method="login"> <result name="success">/WEB-INF/view/login.jsp</result> </action> </package> </struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <a href="toLogin">登录</a> <br> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <form action="login" method="post"> 姓名:<input type="text" name="name"></input> <br> <button type="submit">登 录</button> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <h2>welcome</h2> <h3><s:property value="name"/></h3> </body> </html>
fail.jap
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <h2>fail</h2> </body>
LoginAction.java
<pre name="code" class="java">package struts2InAction; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String name; public void setName(String name) { this.name = name; } //必须写,要不然在页面得不到 public String getName() { return name; } public String login() { if("zs".equals(name)) { setName(name); System.out.println(">>>>"+name); return "success"; } else { return "fail"; } } }
ToLoginAction.java
package struts2InAction; import com.opensymphony.xwork2.ActionSupport; public class ToLoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; public String login() { return "success"; } }
改进:
ToLoginAction.java只做了转发页面的功能,可以删除。
只需把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.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="struts2InAction.LoginAction" method="login"> <result name="success">/WEB-INF/view/success.jsp</result> <result name="fail">/WEB-INF/view/fail.jsp</result> </action> <action name="toLogin" > <result >/WEB-INF/view/login.jsp</result> </action> </package> </struts>