Struts2、Guice 和 index.jsp

Struts2, Guice and index.jsp

使用类似于 Guice 项目 here, and web.xml here.

示例的 GuiceServletContextListener 子类

我发现当浏览到根目录(例如 localhost:8080)时,永远不会访问 webapp 的 index.jsp。考虑到 web.xml:

的配置,这是正确的
  <welcome-file-list>
    <welcome-file>index</welcome-file>
  </welcome-file-list>

(见下文web.xml

您可能已经注意到 index.jsp 文件缺少后缀 .jsp。这是因为 web.xml 添加了安全约束,正如 Struts2 文档 here 所建议的那样。如果添加后缀或省略欢迎文件列表,将启动安全限制。

因此,我必须以(看起来像的)和奇怪的方式配置 Struts2 才能使应用程序正常工作:

<action name=""/>

(见下文struts.xml

问题

Struts2配置是否正确?或者我应该以不同的方式配置 Struts2 吗?如果是,是什么?

感谢收到的任何帮助。

设置

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>PVS Web Application</display-name>

  <listener>
    <listener-class>org.veary.pvs.web.guice.GuiceListener</listener-class>
  </listener>  

  <filter>
    <filter-name>guice</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

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

  <welcome-file-list>
    <welcome-file>index</welcome-file>
  </welcome-file-list>

  <security-constraint>
    <display-name>No direct JSP access</display-name>
    <web-resource-collection>
      <web-resource-name>No-JSP</web-resource-name>
      <url-pattern>*.jsp</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>no-users</role-name>
    </auth-constraint>
  </security-constraint>

  <security-role>
    <description>Don't assign users to this role</description>
    <role-name>no-users</role-name>
  </security-role>

</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"https://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true"/>
    <constant name="struts.ui.theme" value="simple" />

    <package name="pvs-base" namespace="/" extends="struts-default">
      <interceptors>
        <interceptor-stack name="pvsDefault">
          <interceptor-ref name="validationWorkflowStack"/>
        </interceptor-stack>
      </interceptors>
      <default-interceptor-ref name="pvsDefault" />
      <global-results>
        <result>/themes/default/layout.jsp</result>
      </global-results>
      <action name=""/>
    </package>

</struts>

答案可能比我想象的要简单:

  • 修改web.xml使用index.html而不是index.jsp:

    <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>

  • 在新的index.html中使用以下内容:

    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=index">

    <script>top.location='index';</script>

  • 中的struts.xml,改为:

    <action name="index"/>