一、建立以下項目結果css
二、在com.entity包下建立html
1 package com.entity; 2 3 public class User { 4 private String name; 5 private String pwd; 6 7 public User() { 8 } 9 10 public User(String name, String pwd) { 11 this.name = name; 12 this.pwd = pwd; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public String getPwd() { 24 return pwd; 25 } 26 27 public void setPwd(String pwd) { 28 this.pwd = pwd; 29 } 30 31 @Override 32 public String toString() { 33 return "User [name=" + name + ", pwd=" + pwd + "]"; 34 } 35 36 37 38 }
三、在com.action包下建立LoginAction.javajava
1 package com.action; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.entity.User; 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport; 10 /** 11 * 登陸的Action 12 * @author pc 13 * 14 */ 15 public class LoginAction extends ActionSupport { 16 private User user; 17 public String login(){ 18 HttpSession session=ServletActionContext.getRequest().getSession(); 19 20 //登陸成功,將用戶放入session做用域中 21 if(user!=null){ 22 System.out.println("user:"+user); 23 //保存用戶信息到Session中 24 session.setAttribute("user", user); 25 return SUCCESS; 26 }else{ 27 return ERROR; 28 } 29 30 } 31 public User getUser() { 32 return user; 33 } 34 public void setUser(User user) { 35 this.user = user; 36 } 37 38 39 }
四、在com.interceptor包下建立LoginInteceptor.javaweb
1 package com.interceptor; 2 3 import java.util.Map; 4 5 import javax.servlet.http.HttpSession; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.entity.User; 10 import com.opensymphony.xwork2.Action; 11 import com.opensymphony.xwork2.ActionInvocation; 12 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 13 /** 14 * 權限驗證檢查攔截器 15 * @author pc 16 * 17 */ 18 public class LoginInteceptor extends AbstractInterceptor { 19 20 @Override 21 public String intercept(ActionInvocation invocation) throws Exception { 22 //Map<String, Object> session=invocation.getInvocationContext().getSession(); 23 HttpSession session=ServletActionContext.getRequest().getSession(); 24 User user=(User)session.getAttribute("user"); 25 System.out.println("loginInte:"+user); 26 27 if(user==null){ 28 //請求的Action 29 return invocation.invoke(); 30 }else{ 31 return Action.LOGIN; 32 33 } 34 } 35 36 }
五、在src下建立struts.xml文件apache
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <!--1. 中文亂碼處理 --> 5 <constant name="struts.i18n.encoding" value="UTF-8"/> 6 7 <package name="default" namespace="/" extends="struts-default"> 8 <!--2. 配置全部攔截器的節點 --> 9 <interceptors> 10 <!-- 定義權限驗證攔截器 --> 11 <interceptor name="myLogin" class="com.interceptor.LoginInteceptor"></interceptor> 12 13 <!-- 定義攔截器棧 --> 14 <interceptor-stack name="myStack"> 15 16 <!-- 引用自定義攔截器 --> 17 <interceptor-ref name="myLogin"/> 18 19 <!-- 引用系統默認攔截器 --> 20 <interceptor-ref name="defaultStack"/> 21 </interceptor-stack> 22 </interceptors> 23 24 <!-- 3.定義默認攔截器 --> 25 <default-interceptor-ref name="myStack"/> 26 27 <!-- 4.定義全局結果 --> 28 <global-results> 29 <result name="login" type="redirect">/login.jsp</result> 30 </global-results> 31 <!-- 5.配置Action=明星 --> 32 <action name="login" class="com.action.LoginAction" method="login"> 33 <result name="success">/index.jsp</result> 34 <result name="error">/error.jsp</result> 35 <!-- 引用攔截器==小工 --> 36 <interceptor-ref name="myStack"/> 37 </action> 38 </package> 39 </struts>
六、在WebRoot下建立login.jsp文件瀏覽器
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <center> 25 <fieldset style="width:300px;"> 26 <legend>登陸</legend> 27 <!-- 在瀏覽器直接請求頁面,攔截器不會發生任何做用, 28 攔截器只針對於Action的請求發生做用 29 也就是login.action走攔截器,直接請求index.jsp不會走攔截器--> 30 <form action="login.action" method="post"> 31 <table> 32 <tr> 33 <td>用戶名:</td> 34 <td><input type="text" name="user.name"/></td> 35 </tr> 36 <tr> 37 <td>密碼:</td> 38 <td><input type="password" name="user.pwd"/></td> 39 </tr> 40 <tr> 41 <td><input type="submit" value="提交"/></td> 42 <td><input type="reset" value="重置"/></td> 43 </tr> 44 </table> 45 </form> 46 </fieldset> 47 </center> 48 </body> 49 </html>
七、在WebRoot下建立index.jsp文件session
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP 'index.jsp' starting page</title> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 </head> 23 24 <body> 25 <h1>操做成功</h1> 26 <s:if test="user.name eq 'holly'"> 27 holly你來啦? 28 </s:if> 29 <s:else> 30 我不認識你?你是誰? 31 </s:else> 32 </body> 33 </html>
八、在WebRoot下建立error.jsp文件app
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 操做失敗! 25 </body> 26 </html>
九、編輯WebRoot下WEB-INF下web.xml文件jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" 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"> 3 <filter> 4 <filter-name>struts2</filter-name> 5 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 6 </filter> 7 8 <filter-mapping> 9 <filter-name>struts2</filter-name> 10 <url-pattern>/*</url-pattern> 11 </filter-mapping> 12 13 <welcome-file-list> 14 <welcome-file>login.jsp</welcome-file> 15 </welcome-file-list> 16 17 </web-app>
十、運行ide
當把login.jsp頁面的form表單的action值改成index.jsp後,再看登陸後的效果