struts2 运行原理和登陆校验
上面我画的也很清楚了,struts2就是这样走下来的,先经过actionmapping,回到控制器,再到ActionProxy,ActonProxy通过Configuration Manager查询struts.xml配置文件找到ActionProxy要代理的acton,找到之后呢,进入Action Invocation 走好多的拦截器,最后到达action,得到Result,跳转到Action或者是Jsp,最后再走一遍拦截器,走到HttpServletResponse,结束
struts2登陆校验的配置文件如下
<?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> <!-- 配置web默认编码集,相当于HttpServletRequest.setCharacterEncoding("UTF-8"); --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 当struts 配置文件修改后,系统是否自动重新加载该文件,默认是false --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 开发者模式 --> <constant name="struts.devMode" value="true"></constant>
<!--先配置package--> <package name="a" extends="struts-default" namespace="/login">
<interceptors> <interceptor name="checkuser" class="com.jikexueyuan.interceptor.CheckUser"/> <interceptor-stack name="userDefault"> <interceptor-ref name="checkuser"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors>
<!--配置默认拦截器--> <default-interceptor-ref name="userDefault"></default-interceptor-ref>
<!--配置全局返回值--> <global-results> <result name="error">/error.jsp</result> </global-results>
<!--配置登陆Action,这个action因为是登陆的所以不能用自己的拦截器拦,要用默认拦截器--> <action name="LoginAction" class="com.jikexueyuan.action.LoginAction" method="login"> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="true">/message.jsp</result> <result name="false">/error.jsp</result> </action> <!--配置普遍Action,这个action要被拦截器拦截,要用自定义的拦截器--> <action name="LookAction" class="com.jikexueyuan.action.LookAction"> <result name="ok">/look.jsp</result> </action> </package> </struts>
文件无法上传,所以先不上传了
CheckUser.java
public class CheckUser implements Interceptor { @Override public void destroy() { // TODO 自动生成的方法存根 } @Override public void init() { // TODO 自动生成的方法存根 } @Override public String intercept(ActionInvocation arg0) throws Exception { // TODO 自动生成的方法存根 HttpSession httpSession = ServletActionContext.getRequest().getSession(); if(httpSession.getAttribute("username")!=null){ String result = arg0.invoke(); System.out.println(result); return result; }else{ return "error"; } } }
LoginAction.java
public class LoginAction { public String login() throws Exception { // TODO 自动生成的方法存根 HttpSession httpSession = ServletActionContext.getRequest().getSession(); String username = user.getUsername(); String password = user.getPassword(); if(username.equals("宋明阳")&&password.equals("1213265442")){ httpSession.setAttribute("username", username); httpSession.setAttribute("password", password); return "true"; } else{ return "false"; } } private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } }
login.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); %> <!DOCTYPE HTML> <html> <head> <title>login.jsp</title> </head> <body> <form action="<%=path%>/login/LoginAction.action" method="post"> <input type="text" name="user.username" required/> <input type="password" name="user.password" required/> <input type="submit" value="登陆"> </form> <a href="<%=path%>/login/LookAction.action">看看里面有什么</a> </body> </html>
其他的页面无关紧要,就不上传了