Struts2,Guice和index.jsp

问题描述 投票:0回答:1

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

我发现浏览到root时从不访问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

veb.hml

<?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>
java web-applications struts2 guice
1个回答
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"/>
© www.soinside.com 2019 - 2024. All rights reserved.