49212

HTTP 404 error in my first Struts2 application

Question:

I have made a simple struts 2 application for my learning purpose. I have followed the tutorial from youtube. But after completing my program, when I run it, it is showing HTTP 404 error when I enter the URL: http://localhost:1443/Struts2Example/hello.action.

Following is my web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Struts2Example</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.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>

Following is my Action Class

package com.subir.struts2.action; public class getAction { public String execute() { System.out.println("Hello !execute method "); System.out.println("Hello from get getAction!!"); return "success"; } }

Following is my 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"></constant> <package name="default" extends="struts-default"> <action name="hello" class="com.subir.struts2.action.getAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package>

Following is my success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> Success Page!!! </body> </html>

Following is my error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> Error page!!! </body> </html>

Following is my directory structure.

<a href="https://i.stack.imgur.com/yYvTm.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/yYvTm.png" data-original="https://i.stack.imgur.com/yYvTm.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Please let me know what might be the issue causing this error.

Answer1:

1- In your web.xml you have to close </web-app>

2- In your struts.xml you have to close </struts>

3- you must add struts 2 Deploy path in Web Deployment Assembly in Eclipse like in this picture <a href="https://i.stack.imgur.com/VmZv6.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/VmZv6.jpg" data-original="https://i.stack.imgur.com/VmZv6.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

NB: the first letter in class name in JAVA it should be uppercase letter instead of class getAction you should use class GetAction or other name which starts with uppercase letter.

I tested your code with my configuration struts 2.3.24, Tomcat 8.0, jdk 1.8, with those rectification, it work without problems.

<a href="https://i.stack.imgur.com/hI74B.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/hI74B.jpg" data-original="https://i.stack.imgur.com/hI74B.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

In console

<a href="https://i.stack.imgur.com/oKQQt.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/oKQQt.jpg" data-original="https://i.stack.imgur.com/oKQQt.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • How to pass object data between action class and jsp page?
  • Conversation Plugin vs ActionFlow
  • Invoking a Javascript from Lotus Notes on click of a button
  • jQuery - events won't fire for dynamically created tab elements
  • Struts2 simple Application not executing
  • How can I configure HTTP Response Headers in a Struts2 Interceptor?
  • app_Offline.htm doesn't work for subfolders of my application in IIS
  • Type mismatch: cannot convert from Connection to Connection
  • Filtering SPARQL results by day and month
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Spring MVC redirect with custom http headers
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • Is it mandatory to have a doGet or doPost method?
  • How does the HEXTORAW() function work? What is the algorithm?
  • File extension of zlib zipped html page?
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • How to convert SOAP response with xsi values to json in WSO2esb
  • Jetty 9 HashLoginService
  • Problem deserializing objects from cache on MyBatis 3/Java
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Cannot connect to cassandra from Spark
  • Spring security and special characters
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • retrieve vertices with no linked edge in arangodb
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Java static initializers and reflection
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • JaxB to read class hierarchy
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases