1, Struts2 's predecessor is the Opensymphony Webwork2, actually strut and WEBWORK2 merged to form STRUTS2.
2. A helloword example1) to create a Web application, the jar package required is: 2) configuration STRUTS2 is configured in the. xml file as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version=" 3.0 "><display-name>struts2_helloworld< /display-name> <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> <welcome-file-list> <welcome-file> Index.jsp</welcome-file> </welcome-file-list></web-app>
3) Create the control class HelloWorld
Package Com.yyq.action;import Com.opensymphony.xwork2.actionsupport;public Class HelloWorld extends Actionsupport { private String message; @Override public String execute () throws Exception { message = "Hello world,struts2"; return SUCCESS; } Public String GetMessage () { return message; }}
4) Create index.jsp
<%@ page contenttype= "Text/html;charset=utf-8"%><%@ taglib prefix= "s" uri= "/struts-tags"%>
5) Configure HelloWorld. There are three main ways to implement a connection in STRUTS2: Use an XML file for configuration, configure using annotation, and use COC to contract naming. Create a new file named Struts.xml in the SRC directory, configured as follows:<?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> <package name=" action "namespace="/"extends=" Struts-default "> <action name= "hwaction" class= "Com.yyq.action.HelloWorld" > <result>/index.jsp</result > </action> </package></struts>
6) Launch Tomcat, publish and run HelloWorld, enter: http://localhost:8080/HWAction.action
7) Project structure: STRUTS2 is a framework in which the framework itself increases the complexity of the software by adding more layers, which is more expensive than a pure servlet or JSP. But layering can make each module clearer and more collaborative for many people to do development. And the use of hierarchical structure, can make the division of software Development more meticulous, can enhance the readability and maintainability of the software. When introducing STRUTS2, it takes only a low learning cost to get an efficient development framework that is important both for developers and managers. Using STRUTS2 doesn't mean developers can easily develop without writing complex code, and Struts2 doesn't mean developers can easily develop without having to write complex code. Struts2 only blocks hierarchies, but complex business logic needs to be written by the developer in person.3. A simple User Login example
1) Create the user Pojo class file:Package Com.yyq.pojo;public class User { private String userName; private String password; Public User () { } public String GetUserName () { return userName; } public void Setusername (String userName) { this.username = userName; } Public String GetPassword () { return password; } public void SetPassword (String password) { this.password = password; }}
2) Create the action class file:
Package Com.yyq.action;import Com.opensymphony.xwork2.actionsupport;import Com.yyq.pojo.user;public class Login Extends Actionsupport { private user user; Public User GetUser () { return user; } public void SetUser (user user) { this.user = user; } Execute logon method public String execute () { return SUCCESS; }}
3) Create landing page index.jsp:
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
4) Create Login Success Page welcome.jsp:
<%@ taglib prefix= "s" uri= "/struts-tags"%><%@ page contenttype= "Text/html;charset=utf-8" language= "java"% >
5) Configure the Web. xml file:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version=" 3.0 "> <display-name>struts2_ helloworld</display-name> <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> <welcome-file-list> < Welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
6) Configure the Struts.xml file:
<?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> <package name=" action "namespace="/"extends=" Struts-default "> <action name= "Login" class= "Com.yyq.action.Login" > <result>/welcome.jsp</result> </action> </package></struts>
7) Start Tomcat, enter http://localhost:8080/index.jsp
Enter your user name and password at random, and click Login:
8) Project Structure diagram:
Reprint the first code after theJava Web struct Primer Basics