The web.xml file:
The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.
As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder WebContent/WEB-INF.
<web-app id="WebApp_9" 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_5.xsd">
Struts2FirstProject
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
default.jsp
Action Class:
package org.arpit.javapostsForLearning;
public class TutorialAction {
public String execute()
{
String success="success";
return success;
}
}
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Insert title here
Hello world!!!This is Strut2 tutorial
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Insert title here
Error!!!!
The struts.xml file:
So if client enters “http://mywebapp/getTutorial” then Tutorial action will be called.
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
TutorialView.jsp
Error.jsp
- Request(http://localhost:8080/Strut2FirstProject/getTutorial) is generated by user and sent to Servlet container.
- Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.In this project,getTutorial action goes to TutorialAction class.
- In tutorialAction,execute() method is executed and returns “success”
- As per mapping in struts2.xml,result name is matched with returned string of execute().
- In this case,”success” is matched with result name=”success” in struts2.xml so accordingly,TutorialView.jsp is rendered and returned to user.
Now finally we will run our project.
right click on project->run as->run on server


















