One 、 take struts 2 Of lib Under the folder commons-fileupload.jar、commons-io.jar、freemarker.jar、javassist.jar、ognl.jar、struts2-core.jar and xwork-core.jar The required classes are copied to web Applied WEB-INF/lib Under the path . If you need to web Use in application struts 2 More features of , Then the corresponding JAR File copy to web Applied WEB-INF/lib Under the path .

Two 、 edit Web Applied web.xml The configuration file , To configure Struts 2 At the heart of Filter. stay web.xml Define the core of Filter To intercept user requests .

because Web Applications are based on requests / Response building applications , So no matter which MVC Web frame , Need to be in web.xml Configure the core of the framework in filter perhaps servlet, Only in this way can the framework be involved Web Application .

Here is struts 2 stay web.xml Configuration fragment in file :

  1. <?xml version="1.0" encoding="GBK"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  6. <!--  Definition Struts 2 At the heart of Filter -->
  7. <filter>
  8. <filter-name>struts2</filter-name>
  9. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  10. </filter>
  11. <!--  Give Way Struts 2 At the heart of Filter Intercept all requests  -->
  12. <filter-mapping>
  13. <filter-name>struts2</filter-name>
  14. <url-pattern>/*</url-pattern>
  15. </filter-mapping>
  16. </web-app>
<?xml version="1.0" encoding="GBK"?>
<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">
<!-- Definition Struts 2 At the heart of Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <!-- Give Way Struts 2 At the heart of Filter Intercept all requests -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

3、 ... and 、 If you need to POST Way to submit a request , You need to define jsp page . If only with GET Way to send request , You don't need to .

Four 、 Define the Action class

This is all. MVC Essential in the framework . because Action Namely MVC The controller in the framework . The controller is responsible for calling model To process the request .

MVC The underlying mechanism of the framework is : The core servlet perhaps Filter After receiving the user's request , Usually, the user's request is simply preprocessed , For example, parsing 、 Package parameters, etc , And then create... Through reflection Action example , And call Action To handle the user's request .

But when Servlet or Filter After intercepting the request , How does it know to create that Action Example ? There are generally two ways to solve it

1: Using configuration files . Such as : We can configure login.action Corresponding use LoginAction class . So that we can MVC The framework knows how to create that Action An example of .

2: Take advantage of the Convention . Such as : We can make an agreement xxx.action Always corresponds to XxxAction class . If the core controller receives the request , The corresponding Action Class to handle user requests .

5、 ... and 、 To configure Action

To configure Action That is to specify which request corresponds to which 、Action To deal with , So that the core controller can create the appropriate Action example , And call the Action The business control method of .

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
  4. "http://struts.apache.org/dtds/struts-2.1.7.dtd">
  5. <!--  Appoint struts 2 The following elements of the configuration file   -->
  6. <struts>
  7. <!--  be-all Action Definitions should be placed in package Next  -->
  8. <package name="mystruts" extends="struts-default">
  9. <action name="login" class="com.action.LoginAction" method="execute">
  10. </action>
  11. </package>
  12. </struts>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Appoint struts 2 The following elements of the configuration file -->
<struts>
<!-- be-all Action Definitions should be placed in package Next -->
<package name="mystruts" extends="struts-default">
<action name="login" class="com.action.LoginAction" method="execute">
</action>
</package>
</struts>

6、 ... and 、 Configure the correspondence between processing results and physical view resources

When Action After processing the user request , It usually returns a processing result . We can think of the name as a logical view name , This view name needs to be associated with the specified physical view resource to be valuable . So we also need to configure the correspondence between the processing results .

  1. <action name="login" class="com.action.LoginAction" method="execute">
  2. <!--  Define the mapping between three logical views and physical resources  -->
  3. <result name="input">/login.jsp</result>
  4. <result name="error">/error.jsp</result>
  5. <result name="success">/success.jsp</result>
  6. </action>
<action name="login" class="com.action.LoginAction" method="execute">
<!-- Define the mapping between three logical views and physical resources -->
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/success.jsp</result>
</action>

7、 ... and 、 Write view resources

Through these steps , We can basically complete a struts 2 Configuration of

Here's a complete struts 2 Example

1、 Configuration core Filter

The code is the same as above

2、 To write jsp page : Name it login.jsp; Realize the basic landing function

  1. <%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%>
  2. <html>
  3. <head>
  4. <title> The user login </title>
  5. </head>
  6. <body>
  7. <form action="login">
  8. user name :<input type="text" name="username" />
  9. The secret    code :<input type="password" name="password" />
  10. <input type="submit" value=" land ">
  11. </form>
  12. </body>
  13. </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title> The user login </title>
</head> <body>
<form action="login">
user name :<input type="text" name="username" />
The secret code :<input type="password" name="password" />
<input type="submit" value=" land ">
</form>
</body>
</html>

3、 To configure Action  . Name it struts.xml  And put it on src Under the directory of

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
  4. "http://struts.apache.org/dtds/struts-2.1.7.dtd">
  5. <!--  Appoint struts 2 The following elements of the configuration file   -->
  6. <struts>
  7. <!--  be-all Action Definitions should be placed in package Next  -->
  8. <package name="mystruts" extends="struts-default">
  9. <action name="login" class="com.action.LoginAction" method="execute">
  10. <!--  Define the mapping between three logical views and physical resources  -->
  11. <result name="input">/login.jsp</result>
  12. <result name="error">/error.jsp</result>
  13. <result name="success">/success.jsp</result>
  14. </action>
  15. </package>
  16. </struts>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Appoint struts 2 The following elements of the configuration file -->
<struts>
<!-- be-all Action Definitions should be placed in package Next -->
<package name="mystruts" extends="struts-default">
<action name="login" class="com.action.LoginAction" method="execute">
<!-- Define the mapping between three logical views and physical resources -->
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

4、 Write to handle user requests Action class

  1. package com.action;
  2. public class LoginAction{
  3. private String username;
  4. private String password;
  5. public String getUsername() {
  6. return username;
  7. }
  8. public void setUsername(String username) {
  9. this.username = username;
  10. }
  11. public String getPassword() {
  12. return password;
  13. }
  14. public void setPassword(String password) {
  15. this.password = password;
  16. }
  17. // Define the login Method
  18. public String execute(){
  19. // adopt ActionContext() Set up session Properties of the range
  20. ActionContext.getContext.getSession().put("user", getUsername());
  21. if(getUsername().equals("chentmt")&&getPassword().equals("chenssy")){
  22. // adopt ActionContext() Set up reque Properties of the range
  23. return "success";
  24. }
  25. else {
  26. return "error";
  27. }
  28. }
  29. }
package com.action;
public class LoginAction{
private String username;
private String password;
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;
} // Define the login Method
public String execute(){
// adopt ActionContext() Set up session Properties of the range
ActionContext.getContext.getSession().put("user", getUsername()); if(getUsername().equals("chentmt")&&getPassword().equals("chenssy")){
// adopt ActionContext() Set up reque Properties of the range
return "success";
}
else {
return "error";
}
}
}

After configuring these resources , We can access it through the browser :http://localhost:8080/struts2_01/login.jsp You can see the following interface

           Reading Li Gang 《 Lightweight java EE Enterprise application practice ( The third edition )—struts
2+Spring 3+Hibernate Integrated development 》

struts 2 Reading notes -----struts2 More articles on the development process of

  1. TIJ Reading notes 02- Control the execution process

      TIJ Reading notes 02- Control the execution process TIJ Reading notes 02- Control the execution process if-else iteration An unconditional branch switch sentence The execution path of all conditional statements is determined by the true or false of the conditional expression , That is to say, it is decided by Boolean test results ...

  2. Struts2 Study note 2 : Development process

    One : Create project , Add dependency package Two : stay web.xml Configure the core controller <filter> <filter-name>struts2</filter-name> <fil ...

  3. Struts 2 Reading notes -----Action visit Servlet API

    Action visit Servlet API Struts2 Medium Action Not with any Servlet API coupling , So the framework is more flexible , Easier to test . about Web For the controller of the application , Do not visit ServletAPI How many ...

  4. Struts2 Technology insider Reading notes 2 web The basic model of development

    Best practices Before we discuss the basic model , Let's start with one word : Best practices Any program must follow a specific specification . This kind of norm has a common name, such as : The package name is all lowercase , The first letter of each word in the class name is capitalized, and so on ; There are other things that we need to strictly abide by ...

  5. [ Reading notes ] Web The way of front-end development and cultivation

    Original address :http://www.cnblogs.com/bnbqian/p/3735565.html Reprint please indicate the source The book we are going to read today is Web The way of front-end development and cultivation The first 1 Chapter Starting from website reconstruction 1.1 Perishing ...

  6. 《 Elephant Thinking in UML》 Reading notes : software development —— From real world to object world

    Reference resources :Process-oriented vs. Object-oriented Preface The software industry is adopting OO After my thoughts , From the beginning, only coding was used OO, Up to now “ analysis - Design - code ” All links use OO, To form the OOA.O ...

  7. flask Reading notes -flask web Development

    During application startup , Flask Will create a Python Of logging.Logger Class instance , And attach it to the application instance , adopt app.logger visit ===================== ...

  8. Struts2 Application development process

    Struts2 Development process In order to be able to eclipse Use in Struts2 In development , We need to import some related information as needed jar package : Download the relevant compressed package on the official website , Here are two :struts-2.3.30-all.z ...

  9. One Struts2 Development process

    SSH And SSM brief introduction SSM:Spring+SpringMVC+MybatisSSH:Struts2+Hibernate+SpringStruts2: It's a framework that focuses on the control layer Hibernate: It's a ORM( ...

Random recommendation

  1. ordinary SQL Joint table update

    UPDATE dbo.bankinfo1 SET bankinfo1.BankName=BankInfo.BankName FROM BankInfo where bankinfo1.banknumb ...

  2. cacti application

    cacti By a lot of IDC/CDN Provide commercial bandwidth computing use : Bandwidth 95 charging (95th Percentile charging) 95 The billing method is CDN Common billing methods : CDN It's basically a monthly settlement . Every time 5 Take one minute ...

  3. C# Realization JSON Serialization and deserialization

    1. Use JavaScriptSerializer Class implementation serialization namespace: System.Web.Script.Serialization eg: // serialize private string ...

  4. Baidu interview questions String similarity Algorithm similar_text And page similarity algorithm

    The interview in Baidu , It's like trying to be cruel . First, during the period when the interviewer looks at the resume , Except for a self-defined string similarity , And write the algorithm of similarity . ... I really haven't heard of this ,php Of similar_text Functions are also unheard of . See before s ...

  5. python Three ways to access dictionaries

    Definition dictionary dic = {'a':"hello",'b':"how",'c':"you"} Method 1 : for key in dic: prin ...

  6. EasyUI-Combox

    Combox Data format and default selected item settings [{ "id":1, "text":"text1" },{ "id":2, &q ...

  7. hadoop function wordcount example ,hdfs Simple operation

    1. see hadoop edition [hadoop@ltt1 sbin]$ hadoop version Hadoop -cdh5.12.0 Subversion http://github.com/cloud ...

  8. WinForm China is in Africa UI Thread changes the value of the control

    Never UI Thread calls UI Control assignment . Or other updates UI If you want to operate it , There will be anomalies : System.InvalidOperationException:“ Invalid inter thread operation : Never create controls “xxx” Thread access it .” ...

  9. 【BZOJ1417】Pku3156 Interconnect

    Answer key : A simple question Obviously, we just need to know the size of each link block And found that x1+xn=30 among xi<=xi+1 The only state of being 5000 So direct memory search is OK emm original map You can also map ve ...

  10. pandas Split groupby Apply a function apply And aggregate results aggregate

    https://www.jianshu.com/p/2d49cb87626b df.groupby('A').size()