How can I set the welcome page to a struts action?

后端 未结 15 2006
天涯浪人
天涯浪人 2020-11-29 20:15

I have a struts-based webapp, and I would like the default \"welcome\" page to be an action. The only solutions I have found to this seem to be variations on making the welc

15条回答
  •  野性不改
    2020-11-29 20:52

    Just add a filter above Strut's filter in web.xml like this:

    
        customfilter
        com.example.CustomFilter
    
    
        customfilter
        /*
    
    

    And add the following code in doFilter method of that CustomFilter class

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;
        if (! httpResponse.isCommitted()) {
            if ((httpRequest.getContextPath() + "/").equals(httpRequest.getRequestURI())) {
                httpResponse.sendRedirect(httpRequest.getContextPath() + "/MyAction");
            }
            else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }
    

    So that Filter will redirect to the action. You dont need any JSP to be placed outside WEB-INF as well.

提交回复
热议问题