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;
}
}