Before we come to the story of Struts2, let‘s first look at what MVC is.
Model -
The lowest level of the pattern which is responsible for maintaining data
View -
This is responsible for displaying all or a portion of the data to the user.
Controller -
Software Code that controls the interactions between the Model and View.
What is Struts2
Struts2 is a popular and mature web application framework based on the MVC design pattern
Environment set up and first Application using Struts 2
1. Download the needed jar files from the web.
I used the version struts-2.5.20
2. Create Dynamic web project and import the needed jars.
At the moment, we don‘t need all the jars under the lib folder of the struts, I have only import the required jars for our simple demo.
import those jars to /WebContent/Web-INF/lib
3. Create Web.xml under WebContent/WEB-INF
we need to define filter and filter-mapping in WEB.xml file.
the puporpose of this is to tell the server, "Let Struts handle the requests"
<?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>Struts2</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.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4. create a simple jsp .
in my jsp page, it will print "this is result.jsp"
and the variable "result" defined in the HelloWorldAction class.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri ="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> this is result.jsp <s:property value="result"/> </body> </html>
5. Create a HelloWorldAction Class and ServiceTest Class
we can define our Service in the method execute()
in my simple example I have defined a service class "ServiceTest"
it will return a very simple String as result.
package com.yang; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String result = null; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String execute() { ServiceClassTest t = new ServiceClassTest(); result = t.ServiceTest(); setResult( result); System.out.println(result); return SUCCESS; } }
package com.yang; public class ServiceClassTest { public String ServiceTest() { return "This is a Service Test"; } }
6. create the struts.xml
as could see, there is a action defined, and a mapped class HelloWorldAction.
NameSpace value means: http://<server>:<port>/<webapp>/<namespace>/<action>.action
if it is not defined, it will use default : http://<server>:<port>/<webapp>/<action>.action
the result page in my example would be result.jsp
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name ="default" namespace="/tutorial" extends="struts-default"> <action name = "helloworld" class = "com.yang.HelloWorldAction"> <result>/result.jsp</result> </action> </package> </struts>
Output:
As could see,
I have used url: localhost:8080/Struts2/tutorial/helloworld
Struts2 is my WebProject name
tutorial is the namespace defined.
helloworld is the action name defined.
Struts has successfully received the request, and routed to the page result.jsp, at the same time calling the
action class "HelloWorldAction", and printed out the variable value sent from the test Service.