0

I am trying to run a web application example on struts2 and i am facing a problem. Here are the codes 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>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>/*</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</h1>
       <form action="abc">

      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

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="helloworld" extends="struts-default namespace="/">

      <action name="abc" 
            class="com.junaid1.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">HelloWorld.jsp</result>
            <result name="error">/oops.jsp</result>
      </action>
   </package>
</struts>

HelloWorldAction.java

package com.junaid1.struts2;

public class HelloWorldAction{
       private String name;

       public String execute() throws Exception {
          return "success";
       }

       public String getName() {
          return name;
       }

       public void setName(String name) {
          this.name = name;
       }
    }

When i execute this project on tomcat server it displays the index.jsp page just fine .This page has a form . when i press the submit button it gives an error saying "the requested resource is not available" with a message "/test1/abc" . abc is the name of the action and this action is not being called . I have searched a lot before posting this question . At first i thought i might be missing some libraries but this does not seem to be the issue now to me as i am using maven for dependency management .Here is my pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
      </plugin>  
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.16</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
  </dependencies>
</project>

Thanks in advance . link to the project structure is s17.postimg.org/axpn8cxbz/error.png

sehe
  • 374,641
  • 47
  • 450
  • 633
Junaid Shirwani
  • 360
  • 1
  • 6
  • 20

2 Answers2

1

You can see the index page because it's shown by the web container Tomcat, and it doesn't have Struts tags (like any other web project without Struts framework).

The browser url shows /test1/. Then you are making request /test1/abc by pressing a submit button, and it was filtered by struts2, but it couldn't find action with name abc neither in namespace / nor in default namespace, and you don't have any resource with that path, so you fairly got 404 error.

You might say that configured the action with the name abc in the default namespace. But this configuration is not available at runtime, also the package name should be struts-default which your package extends.

The struts.xml file should be on classpath, it means that in src or resources folder. When compiling it's copied to compiler output folder. After building it will be copied to WEB-INF/classes from the compiler output folder. These two folders are temporarily made and could be deleted before the process, so if you don't have struts.xml in source folder then you are missing this file when you run your application.

Also note, that you don't have any JSP pages in the webcontent folder that you used in the results.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I placed the struts.xml files in the src folder . i have placed the jsp files in the web content folder . actions are still not working and i have a new error when i access http://localhost:8080/test1/HelloWorld.jsp.I was able to access it before . "HTTP Status 500 - The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] " – Junaid Shirwani Oct 27 '14 at 17:17
  • It's already different error, first you should check `web.xml` file in the `WEB-INF`, then change the filter because it's deprecated in the version of Struts you use. – Roman C Oct 27 '14 at 17:30
  • I have already changed the filter .this is the filter i am using now "org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter" I googled and i learned that i had to use this filter . is my project structure still not correct .I am following this tutorial "http://www.tutorialspoint.com/struts_2/struts_examples.htm" and i have done exactly as it says aand changed whatever you guys asked . Can you help me solve it on team viewer . I am willing to share my computer with you – Junaid Shirwani Oct 27 '14 at 17:33
  • you might have mapped the filter incorrectly, you can update the question. – Roman C Oct 27 '14 at 17:38
  • why cant it still find the action abc when the struts.xml file is in the src folder ? – Junaid Shirwani Oct 27 '14 at 17:42
  • because `struts.xml` is wrong, you haven't updated it, you should also use 2.3 version of dtd. – Roman C Oct 27 '14 at 17:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63720/discussion-between-junaid-shirwani-and-roman-c). – Junaid Shirwani Oct 27 '14 at 17:47
0

At first you should use in web.xml

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

as org.apache.struts2.dispatcher.FilterDispatcher is deprecated.

Please take a look at this example.

njjnex
  • 1,490
  • 13
  • 23
  • This is the right filter to declare when using Struts2 > 2.1.8. Since you are using 2.3.16, that's it. Probably, it isn't enough, though. Your DTD are messed: 2.5 and 3.0 mixed... use the one supported by your Tomcat. Also read this: http://stackoverflow.com/a/19537982/1654265 P.S: @njjnex: ActionForm has nothing to do with Struts2... +1 for the filter, -1 for ActionForm, = 0 :) You probably meant ActionSupport – Andrea Ligios Oct 23 '14 at 21:49