Search code examples
javaweb-applicationsstruts2guice

Struts2, Guice and index.jsp


Using a GuiceServletContextListener subclass similar to the example from the Guice project here, and web.xml here.

I find that the webapp's index.jsp is never accessed when browsing to the root (e.g. localhost:8080). This is correct considering the configuration of the web.xml:

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

(see web.xml below)

You may have noticed the lack of a suffix .jsp for the index.jsp file. This is because of the security constraints added to the web.xml as advised by the Struts2 documentation here. If the suffix is added or the welcome file list ommited, the security constraint will kick in.

So, I have had to configure Struts2 in (what looks like) and odd way to make the application work:

<action name=""/>

(see struts.xml below)

Questions

Is the Struts2 configuration correct? Or should I be configuring Struts2 differently?. If so, what?

Any help gratefully received.

Set Up

  • Java 8
  • Tomcat 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>

Solution

  • The answer is perhaps easier than I thought:

    • Modify the web.xml to use index.html rather than index.jsp:

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

    • In the new index.html use the following:

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

      or

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

    • In the struts.xml, change to the following:

      <action name="index"/>