【问题标题】:Struts2, Guice and index.jspStruts2、Guice 和 index.jsp
【发布时间】:2019-07-27 03:56:21
【问题描述】:

使用 GuiceServletContextListener 子类,类似于 Guice 项目 here 中的示例,以及 web.xml here

我发现 webapp 的 index.jsp 在浏览到根目录时永远不会被访问(例如 localhost:8080)。考虑到 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
  • 雄猫 9
  • Struts 2.5.20
  • Guice 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>

【问题讨论】:

  • stackoverflow.com/q/39399/438992 等等。你不能指望它神奇地理解你想要它做什么,一个空的结果也无济于事。如果您想将其配置为作为欢迎页面转到某个操作,那么您要么需要这样做,要么从 JSP 重定向,等等。
  • 感谢@DaveNewton。但是,链接的答案对我不起作用。由于 JSP 在 Sturts2 文档中的安全约束范围内。因此,一个人得到一个未经授权的响应——JSP 中的任何内容都不会被执行。 'global-results' 用于操作。上面的代码/配置有效,但对我来说它没有通过“气味测试”。
  • 那个问题有多个答案;他们中的大多数都可以正常工作 AFAICT。

标签: java web-applications struts2 guice


【解决方案1】:

答案可能比我想象的要容易:

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

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

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

    &lt;META HTTP-EQUIV="Refresh" CONTENT="0;URL=index"&gt;

    &lt;script&gt;top.location='index';&lt;/script&gt;

  • struts.xml中,改成如下:

    &lt;action name="index"/&gt;

【讨论】:

    猜你喜欢
    • 2011-03-12
    • 2015-03-24
    • 1970-01-01
    • 2012-12-21
    • 2013-07-09
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多