2

I'm new to Struts and I have tried to build the basic application in Struts2. The Web app is giving me 404 Error as the the It cannot find the result or action class. Below are the code snippets of various files. Thank you in advance for help

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="DemoStrtus" method="post">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Basic Struts2</display-name>
    <welcome-file-list>
        <welcome-file>Index.html</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>

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="demostruts" extends="struts-default" namespace="/">
        <action name="demostruts" class="com.demostruts.DemoStrtus" method="execute">
        <result name="success">/DemoStrtus.jsp</result>
        </action>
    </package>
</struts>

DemoStrtus.java (action class)

package com.demostruts;

public class DemoStrtus{
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public String execute() throws Exception
{
    return "success";

}
}

DemoStrtus.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Output</title>
</head>
<body>
<s:property value="name"/>
</body>
</html>

When I run the project. It displays the form. After i enter my name in the form, and press Submit. It gives me 404 error.

enter image description here

My Project explorer looks like

enter image description here

Struts jar file i have added

enter image description here

console warning

May 16, 2018 7:36:25 PM com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
WARNING: Could not find action or result: /DemoStruts/DemoStrtus
There is no Action mapped for namespace [/] and action name [DemoStrtus] associated with context path [/DemoStruts]. - [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Can you also include your `pom.xml` file? It looks as though Struts 2 isn't responding properly. Note that I see nothing obvious wrong with your setup. – Tim Biegeleisen May 16 '18 at 06:19
  • I have not written pom.xml for this project. Do you want to add pom.xml from Struts2 src folder here? – Darshan_Mistry May 16 '18 at 06:50
  • I assumed you are using a Maven project. If not, and you are certain you have the Struts JARs on your classpath, then ignore my comment. – Tim Biegeleisen May 16 '18 at 06:52
  • I'm not using Maven project. I have included jar files in class path as well as in Deployment assembly. – Darshan_Mistry May 16 '18 at 14:05
  • Then I don't know what's wrong. I upvoted your question, hopefully someone else can assist you. I would have to build your entire project locally to find the bug, and I can't do that at the moment. – Tim Biegeleisen May 16 '18 at 14:06
  • Thats ok, Thank you for your help. – Darshan_Mistry May 16 '18 at 14:07
  • You should really use Maven or an equivalent--manual transitive dependency management is fraught with difficulty. The first obvious problem is you're making a request to an action that isn't configured: you've named it `demostruts` which is not what you're requesting. – Dave Newton May 16 '18 at 15:40
  • Yes Dave, The url is different than i thought, but my Struts.xml has the demostruts package with its class mapp and the action name should be displayed as url. I do not know why the url is changing. @DaveNewton – Darshan_Mistry May 17 '18 at 00:02

1 Answers1

0

Thank you for your help and support The mistake was i have set the wrong environment path for java.