如何从doFilter方法调用struts动作

时间:2014-09-19 06:22:10

标签: struts2 filter web.xml requestdispatcher struts-action

我的struts.xml有以下两项操作

<action name="dologincheck" class="com.platinum.uac.biz.LoginAction" method="doLogin">
    <result name="success" type="tiles">Homepage</result>
    <!--   <result name="success">/Profile/view/ViewProfile1.jsp</result>-->
    <result name="none">/Profile/add/Failure.jsp</result>              
    <result name="mail">/Profile/add/EmailAuthentication.jsp</result>
</action>
<action name="newuseraction" class="com.platinum.uac.biz.NewUserAction" method="newuser">
    <result name="success" >/Profile/add/ProfileDetails.jsp</result>
    <result name="error" >/index.jsp</result>
</action>

我需要从dologincheck方法调用操作(doFilter)。我被困在这里因为我不知道如何从doFilter方法

调用一个动作
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws   
      IOException, ServletException {
    try {
    System.out.println("this is do filter before the jsp page");
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = ((HttpServletResponse) res);
    HttpSession session = request.getSession(true);
    String username, password;
    Cookie[] cookies;
    LoginPojo user = (LoginPojo) session.getAttribute("username");
    if (user == null) {
        cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            username = getCookieValue(cookies, "username");
            password = getCookieValue(cookies, "password");
            System.out.println(username);
            System.out.println(password);
            if (username != null && password != null) {
                LoginPojo pojo = new LoginPojo(username, password);
                results = dao.Login(pojo);
                 session.setAttribute("userName", user); // usersession?

               request.getRequestDispatcher("dologincheck").forward(request,
                response);
            } else {
            request.getRequestDispatcher("/index.jsp").forward(request,
            response);
            }
        } else {
        request.getRequestDispatcher("/index.jsp").forward(request,
        response);
        }
    }
    } catch (Exception e) {
    }
    chain.doFilter(req, res);
    //throw new UnsupportedOperationException("Not supported yet.");
}

web.xml的我的过滤器部分是

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>AutomaticLogin</filter-name>
    <filter-class>com.platinum.uac.biz.AutomaticLogin</filter-class>
</filter>
<filter-mapping>
    <filter-name>AutomaticLogin</filter-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.action</url-pattern>
    <servlet-name>action</servlet-name>  
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

1 个答案:

答案 0 :(得分:1)

在网上搜索后我得到了答案。

使用

response.sendRedirect("dologincheck");

而不是

request.getRequestDispatcher("dologincheck").forward(request, response);