Reputation: 5416
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.
Please let me know what might be the issue causing this error.
Upvotes: 1
Views: 2597
Reputation: 1033
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
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.
In console
Upvotes: 3