struts配置

Posted 但为君故L

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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配置的主要内容,如果未能解决你的问题,请参考以下文章

idea+struts2配置struts.xml时struts-default出错

struts2框架需要哪些配置文件

struts2 配置文件

struts2在tomcat中的配置

Struts2配置拦截器,struts2加载常量时的搜索顺序

Struts2 Convention Plugin ( struts2 零配置 )