Question

I have a Struts2 application initially the jsp pages were at the root of the WebContent directory however it was decided to add a jsp directory to hold the jsp pages.

Since this has been done the pages are not accessible and resulted in HTTP 404 error. I am able to access the welcome file /jsp/index.jsp. However when i post the index page i get HTTP 404 error - /SEP/hello.

I know the code works since once the jsp pages are at the root of the WebContent directory it works. I would like to know how can i configure the application to utilize the jsp directory for accessing jsp pages. Under is a screen shot of the directory structure and relevant code:

Project Structure

Project Structure

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.devMode" value="true" />
   <package name="default" namespace="/jsp/" extends="struts-default">

      <action name="hello" 
            class="org.xxxx.product.registration.model.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>

      </action>
   </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

 <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>/jsp/index.jsp</welcome-file>
   </welcome-file-list>


   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/jsp/*</url-pattern>
   </filter-mapping>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Hello World</title>
</head>
<body>
    <h1>Hello world From Struts2 BY D.Ramdhanie</h1>
    <form action="hello">
        <label for="name">Please enter your name</label><br/>
        <input type="text" name="name"/>
        <input type="submit" value="Say Hello"/>
    </form>
</body>
</html>

HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>
Was it helpful?

Solution

The JSPs could be configured in the results in two ways:

  1. Using an absolute pathname
  2. Using a relative pathname

In the first case you should add /jsp/ prefix to JSPs pathnames. In the second case JSPs are relative to the namespace of the action that is added to the webcontent root to calculate the absolute path. More about namespaces and its configuration you could find here.

OTHER TIPS

jsp content inside of the jsp directory.

in struts.xml you mention it as

<result name="success">/HelloWorld.jsp</result>

that mean you are accessing it from root level.

"/" used for WebContent directory by default. so you should update your result as

<result name="success">/jsp/HelloWorld.jsp</result>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top