0

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>
nzeru
  • 11
  • 1
  • 1
  • 6
  • https://stackoverflow.com/q/39399/438992 etc. You can't expect it to magically understand what you want it to do, and an empty result won't help. If you want to configure it to go to an action as the welcome page then you either need to do that, or redirect from a JSP, or etc. – Dave Newton Mar 05 '19 at 16:16
  • Thanks @DaveNewton. However, the linked answer does not work for me. As JSPs are within the security constraint as per the Sturts2 documentation. Therefore,one gets an unauthorized response - anything in the JSP is not executed. The 'global-results' is used for the action. The above code/config works, but to me it does not pass the 'smell test'. – nzeru Mar 05 '19 at 17:29
  • That question has multiple answers; most of them work just fine AFAICT. – Dave Newton Mar 05 '19 at 18:06

1 Answers1

0

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"/>

nzeru
  • 11
  • 1
  • 1
  • 6