struts配置
配置:
攔截登陸
<?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.multipart.maxSize" value="104857600"/>
<package name="interceptor" namespace="/" extends="struts-default">
<!-- 配置攔截器 -->
<interceptors>
<interceptor name="loginInterceptor" class="com.oak.interceptor.LoginIntercrptor"></interceptor>
<!-- 配置攔截器棧 -->
<interceptor-stack name="mysql">
<!-- 系統攔截器棧,,name值是固定的 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginInterceptor">
<param name="excludeMethods">toLogin,login</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 設置全局攔截器 -->
<default-interceptor-ref name="mysql"></default-interceptor-ref>
<!-- 公共跳轉頁
global-results一般和全局攔截器(比如方法攔截器)
搭配使用 -->
<global-results>
<result name="relogin">
/login.jsp
</result>
</global-results>
</package>
<include file="../../cfg/ajax-struts.xml"></include>
<include file="../../cfg/big-struts.xml"></include>
<include file="../../cfg/bill-struts.xml"></include>
<include file="../../cfg/goods-struts.xml"></include>
<include file="../../cfg/small-struts.xml"></include>
<include file="../../cfg/sup-struts.xml"></include>
<include file="../../cfg/user-struts.xml"></include>
</struts>
普通
<?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>
<package name="goods" namespace="/" extends="interceptor">
<action name="goodslist" class="goodsAction" method="goodspagelist">
<result name="success">
/goods.jsp
</result>
</action>
<!-- 商品模糊查詢 -->
<action name="goodlike" class="goodsAction" method="goodlike">
<result name="success">
/goods.jsp
</result>
</action>
<action name="goodsadd" class="goodsAction" method="goodsadd">
<result name="success" type="redirectAction">
goodslist
</result>
</action>
<action name="goodstoupdate" class="goodsAction" method="goodstoupdate">
<result name="success">
/goodstoupdate.jsp
</result>
</action>
<action name="goodsupdate" class="goodsAction" method="goodsupdate">
<result name="success" type="redirectAction">
goodslist
</result>
</action>
<action name="goodsdelete" class="goodsAction" method="goodsdelete">
<result name="success" type="redirectAction">
goodslist
</result>
</action>
</package>
</struts>
攔截登陸
public class LoginIntercrptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
User s = (User) session.get("u");
if (s==null) {
return "relogin";
} else {
return ai.invoke();
}
}
}
action
public String login(){
System.out.println(123);
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.setAttribute("user", user.getUsername());
System.out.println("user"+user);
//綁定session
ActionContext ac = ActionContext.getContext();
Map<String, Object> session1 = ac.getSession();
u = userService.login(
user.getUsername(),
user.getPassword());
System.out.println("u"+u);
if(u.getUsername() != null){
session1.put("u", user);
return SUCCESS;
}else {
return ERROR;
}
}
struts配置