Struts2 is a web-tier development framework that conforms to the MVC pattern
Get Started program create Web project
Importing JAR Packages
Download the Struts-2.3.15.1-all version of the Struts2 jar package.
Note: In STRUTS2 development, generally the least imported jar package, go to apps under the Struts2-blank sample program in copy
Create index.jsp Page
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><H1>Hello Struts2</H1></Body></HTML>
Create hello.jsp Page
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><H1>Hello Struts2</H1></Body></HTML>
Configuring the STRUTS2 Framework Web. xml
Configure the front-end controller (core controller) in the Web. xml file-----is a filter
Purpose: To allow the STRUTS2 framework to run.
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5"> <Display-name>Struts2_01</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></Web-app>
Struts2.xml
Create a Struts.xml configuration file, which is the STRUTS2 framework configuration file.
Purpose: To struts2 the framework process can be executed.
Name: Struts.xml
Location: src (under classes)
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts> < Packagename= "Default"namespace="/"extends= "Struts-default"> <Actionname= "Hello"class= "Com.cxz.HelloAction"Method= "Say"> <resultname= "good">/hello.jsp</result> </Action> </ Package></Struts>
Create a Helloaction class
requirements, create a method in the Helloaction class that returns a value of type string, note that there are no arguments.
Package Com.cxz; Public class helloaction { public String Say () { System.out.println ("Hello Action Say Method" ); return "Good"; }}
Configuring Helloaction in the Struts.xml file
< Packagename= "Default"namespace="/"extends= "Struts-default"> <Actionname= "Hello"class= "Com.cxz.HelloAction"Method= "Say"> <resultname= "good">/hello.jsp</result> </Action> </ Package>
Add a connection to the index.jsp, test
<href= "${pagecontext.request.contextpath}/hello.action"> Access Struts2 </a>
Run
http://localhost:8080/struts2_01/
STRUTS2 Study (i)