Question

I am new to Struts and I want to execute HelloWorld program, but I am getting "Requested resource not available" error as soon as I write a <filter> tag in web.xml.

Project hierarchy

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_2_5.xsd"
   id="WebApp_ID" version="2.5">

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

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

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

struts.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
   <package name="helloworld" extends="struts-default">
      <action name="hello" class="sagar.practice.HelloWorldAction" method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

I added all required struts jars as a user library.

I am accessing index page as : http://localhost:8080/practice/index.jsp

If I remove <filter> tag from web.xml file I am able access index.jsp page, but if I add <filter> tag it is giving "Requested resource not available" error.

Était-ce utile?

La solution

You should turn on devMode on via setting a constant in the struts.xml. This mode is used if you want to troubleshot some issues like yours. It's more informative because it outputs to the console errors that are hidden in the normal mode.

<constant name="struts.devMode" value="true" />  

After turning it on you may discover that you have wrong or inconsistent library set deployed with your application. You might find a java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils printed to the console, then adjust your library set with missing jars.

The commons-lang3-3.1.jar is required to resolve error.

Autres conseils

Change this content in struts.xml with the following

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   "http://struts.apache.org/dtds/struts-2.3.dtd">

Reference Link : click here

If you are using 2.0 or bigger version in struts. Use the below filter.

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

Do the following change in Web.xml

<welcome-file>/WEB-INF/index.jsp</welcome-file>

And in struts.xml

<result name="success">/WEB-INF/HelloWorld.jsp</result>

It May Help You.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top