web.xml:html
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 <filter> 12 <filter-name>struts2</filter-name> 13 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 14 </filter> 15 16 <filter-mapping> 17 <filter-name>struts2</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 </web-app>
index.jsp:java
1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="login" method="post"> 9 姓名:<input type="text" name="username"><br/> 10 密碼:<input type="text" name="password"><br/> 11 <input type="submit" value="提交"> 12 </form> 13 </body> 14 </html>
struts.xml:web
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="default" extends="struts-default"> 9 <action name="login" class="com.action.LoginAction"> 10 <result name="success">/success.jsp</result> 11 <result name="input">/index.jsp</result> 12 </action> 13 </package> 14 15 </struts>
LoginAction.java:apache
1 import org.apache.struts2.ServletActionContext; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 *@date 2014-4-17 7 */ 8 public class LoginAction extends ActionSupport { 9 10 private String username; 11 private String password; 12 public String getUsername() { 13 return username; 14 } 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 public String getPassword() { 19 return password; 20 } 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 @Override 25 public String execute() throws Exception { 26 System.out.println("hello"); 27 if(username.equals("scott")&& password.equals("tiger")){ 28 ServletActionContext.getRequest().getSession().setAttribute("username",username); 29 return "success"; 30 }else{ 31 return "input"; 32 } 33 } 34 35 }
success.jsp:app
1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 歡迎${username}登陸系統... 9 </body> 10 </html>
注意:jsp
struts.xml 文件能夠到struts2開發包,我這裏是2.1.8.1下的apps目錄下尋找,將struts2-blank-2.1.8.1.war後綴改成rar,而後解壓,WEB-INF--->src--->java--->struts.xml。ide