Struts2,Guice和index.jsp

时间:2019-03-05 16:04:18

标签: java web-applications struts2 guice

使用类似于Guice项目here 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 。这是因为Struts2文档here建议将安全性约束添加到 web.xml 中。如果添加了后缀或省略了欢迎文件列表,则安全约束将生效。

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

<action name=""/>

(请参见下面的 struts.xml

问题

Struts2配置是否正确?还是应该以不同的方式配置Struts2?如果是这样,什么?

非常感谢您的帮助。

设置

  • Java 8
  • Tomcat 9
  • Struts 2.5.20
  • 指南4.2.2

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>

1 个答案:

答案 0 :(得分:0)

答案可能比我想的要容易

  • 修改 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"/>