Struts结果是JSP 404

时间:2016-11-18 08:30:57

标签: spring jsp spring-mvc struts2 http-status-code-404

我是Struts2的新手,刚刚完成了一个无动作映射的问题,我的动作new将会执行,但是当它返回JSP时,总会有 404 。试图设置名称空间和结果如下,但仍然没有让它工作。

struts.xml中

<?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.devMode" value="true" />
  <constant name="struts.convention.result.path" value="/WebContent/jsp/" />

  <package name="default" extends="json-default">
    <action name="testAction" class="tw.com.rrstudio.java.test.TestAction" method="execute">
      <result name="login">index.jsp</result>
      <result name="success">TestAction.jsp</result>
    </action>
  </package>

</struts>

还尝试删除约定结果路径并将结果设置为此

<package name="default" extends="json-default">
  <action name="testAction" class="tw.com.rrstudio.java.test.TestAction" method="execute">
    <result name="login">/jsp/index.jsp</result>
    <result name="success">/jsp/TestAction.jsp</result>
  </action>
</package>

但仍然给出404。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Struts2Test</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-context.xml
      /WEB-INF/spring-mvc.xml
    </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <param-name>actionPackages</param-name>
      <param-value>tw.com.rrstudio.java.test</param-value>
    </init-param>
  </filter>

  <jsp-config>
    <taglib>
      <taglib-uri>HTTP://java.sun.com/jsp/jstl/core</taglib-uri>
      <taglib-location>/WEB-INF/lib/tld/c.tld</taglib-location>
    </taglib>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
  </jsp-config>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>

</web-app>

我的动作java代码

package tw.com.rrstudio.java.test;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport implements ServletRequestAware
{
  private static final long serialVersionUID=1L;
  public HttpServletRequest request;
  private String name="";

  public String execute() throws Exception
  {
    System.out.println("Debug: name="+name);
    System.out.println("Debug: path="+request.getContextPath());
    System.out.println("Debug: path="+request.getServletContext().getRealPath("/"));
    if(name!=null && !"".equals(name))
    {return "success";}
    else
    {return "login";}
  }

  @Override
  public void setServletRequest(HttpServletRequest request)
  {this.request=request;}

  public String getName()
  {return name;}

  public void setName(String name)
  {this.name=name;}
}

当我调用我的动作时,它会打印这样的调试消息,

但总是给我404页面,没有任何消息。

HTTP Status 404 - 

type Status report
message 
description The requested resource is not available.

BTW,这是我的包结构。

+Struts2Test
  +Struts2Test/src
    +tw.com.rrstudio.java.test
      -Struts2Test/src/tw/com/rrstudio/java/test/TestAction.java
  +Struts2Test/build
  +Struts2Test/WebContent
    +Struts2Test/WebContent/img
    +Struts2Test/WebContent/jsp
      -Struts2Test/WebContent/jsp/error.jsp
      -Struts2Test/WebContent/jsp/index.jsp
      -Struts2Test/WebContent/jsp/TestAction.jsp
    +Struts2Test/WebContent/META-INF
    +Struts2Test/WebContent/WEB-INF
      +Struts2Test/WebContent/WEB-INF/classes
        -Struts2Test/WebContent/WEB-INF/classes/logging.properties
        -Struts2Test/WebContent/WEB-INF/classes/struts.xml
        +Struts2Test/WebContent/WEB-INF/lib
        -Struts2Test/WebContent/WEB-INF/spring-context.xml
        -Struts2Test/WebContent/WEB-INF/spring-mvc.xml
        -Struts2Test/WebContent/WEB-INF/struts2-action.xml
        -Struts2Test/WebContent/WEB-INF/web.xml

请提供任何建议或建议,感谢您的帮助。

0 个答案:

没有答案