Struts2 Overview:
Struts2 is a framework used to develop MVC applications. It provides solutions to some common problems in the development process of web application, such as verification of the legality of user input information, unified layout, internationalization, etc. Since there is Struts2, there must be Struts1. However, in essence, Struts2 is not extended from Struts1, and it should be said that it is a WebWork that has changed its brand label.
struts1 VS struts2
Struts2 environment construction
Create a new Model, check Struts2, and then select the library you want to use for Struts2. You can directly select Download in the new page to download, or you can download the jar package required for Struts2 in advance and build your own (here I choose the library I built myself)
http://struts.apache.org/download.cgi#struts2516 You can select the version you want to use on this page. You can also download locally on Wulin.com: //www.VeVB.COM/softs/539810.html
After clicking the next step, name your struts2 project (when naming, it is recommended to change the name after the path of the Content Root in the second column. If you change it in the first column, just change the first column, the other two columns will not be changed)
Click Finish to complete the creation of the Struts2 project. The created project is as follows
IDEA VS Eclipse
Struts2 HelloWorld
After building the development environment of Struts2, we started writing the first Struts2 program. It requires that you enter the user's basic information in a form and click submit and jump to another page to display the information entered by the user. Before learning Struts2, this is very simple for us. You only need to send the page request to Servet to respond to the request to another page, and use EL and JSTL to display the information. For Struts2, this is of course a HelloWorld, and we will use this example to start explaining Struts2's various aspects of knowledge.
Write page code in index.jsp for users to enter basic information, as follows:
Write entity classes based on the form name attribute, and create getXxx and setXxx methods, Customer.java
Write code in struts.xml file to handle struts2 requests
Return to the display page show.jsp
struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="helloWorld" extends="struts-default"><action name="input" method="print"><result name="print">/show.jsp</result></action></package></struts>
Customer.java
package com.struts2.hello.test;/*** Created by shkstart on 2018/03/24.*/public class Customer {private String userName;private String email;private String address;/** getXxx() setXxx() method**/public String print() {System.out.println(new Customer());return "print";}@Overridepublic String toString() {return "Customer{" +"userName='" + userName + '/'' +", email='" + email + '/'' +", address='" + address + '/'' +'}';}}index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Input</title></head><body><table><form action="input.action" method="post"><tr><td>UserName</td><td><input type="text" name="userName"></td></tr><tr><td>Email</td><td><input type="text" name="email"></td></tr><tr><td>Address</td><td><input type="text" name="address"></td></tr><tr><td><input type="submit" value="Submit"></td></tr></form></table></body></html>
show.jsp (only displays Body tag content)
UserName: ${userName}<br>Email: ${email}<br>Address: ${address}<br>Detailed code explanation
Member variables in Customer.java correspond to attributes in index.jsp one by one, and do not ignore upper and lower case;
The displayed member variables in show.jsp are consistent with those in Customer.java, otherwise they cannot be displayed
The form form action value in index.jsp should be set to the name property of the action in the struts.xml file
The method attribute of the action node in the struts.xml file should be String print() method in Custome.java, and the name attribute of the result node is
Return value of String print() method
The value of the result node is the page that responds to after processing the struts2 request.
We will explain the relevant knowledge in detail later on the show.jsp page display. That’s all for our HelloWorld in Struts2. I believe that novices like me will not make any major mistakes if they follow them! !
struts.xml file node
package attribute --- struts2 Use package to organize modules
action attribute--- A struts2 request is an action
One action can have multiple result child nodes, and multiple result child nodes can be distinguished using the name attribute.
The type attribute of the result node defaults to dispatcher (forward), indicating the response type of the result
Action VS action
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.