Struts2 (0): getting started with the struts2 framework

Source: Internet
Author: User

Model 1 and Model 2:

InModel 1In this mode, almost all web applications are composed of JSP pages. JSP pages receive and process client requests and directly respond to requests after they are processed. Use a small number of Javabean to process database connections, database access, and other operations.

Model 1 is easy to implement and is suitable for rapid development of small-scale projects. But from the engineering point of view, its limitations are very obvious: JSP pages have both views and controllers, and control logic and presentation logic are mixed together, this reduces code reusability and increases the scalability and maintenance of applications.

InModel 2In this mode, the servlet acts as the front-end controller to receive requests sent from the client. The servlet only contains the control logic and simple front-end processing. Then, the backend Javabean is called to complete the actual logic processing; finally, forward it to the corresponding JSP page to process the display logic.

Because of the introduction of the MVC model, Model 2 is componentized and is more suitable for large-scale application development, but it also increases the complexity of application development. Originally, an application that can be implemented on a simple JSP page is required. In model2, the application is divided into multiple collaborative work parts. It takes more time to grasp the design and implementation process.

Struts2:

Struts2 is an MVC framework. Let's take a look at the example and analyze the summary:

① Create a web project, introduce the struts2 framework development kit, and place it under the lib directory. Only the most basic seven Development Kits are introduced here. For details, see.

② Configure the core controller in Web. xml.Web. xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><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>

The core controller is a filter. Its implementation class is org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter. This class can be found under the struts2-core-2.1.8.1.jar. Try to find this class, copy the package name and class name, instead of typing on the keyboard. The value of filter-name is random. The URL-pattern is/*, indicating that all requests are filtered. That is, all requests are intercepted by the struts2 framework.

③ To submit a request in post mode, you must compile a JSP page containing form data. If you only send requests in get mode, you can skip the JSP page. In this example, it is a POST request.Login. jsp:

<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

④ Compile the action class for processing user requests.Login. Java:

public class Login {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;}public String execute(){if(getUsername().equals("scott")&& getPassword().equals("tiger")){ActionContext.getContext().getSession().put("username",getUsername());return "success";}return "login";}}

This step is also essential to all MVC frameworks, because this action is the C in MVC, that is, the controller, which is responsible for calling methods in the model to process requests. Because the article only introduces the usage of the struts2 framework, it may not call the model method, but make the action simple to process user requests.

According to the above introduction, it is not difficult to find that in the struts2 framework, the controller is actually composed of two parts, that is, to intercept all user requests.Core ControllerThe strutsprepareandexecutefilter is complete, while the actual business control (such as calling model and returning processing results) isBusiness ControllerXxxaction processing.

⑤ Create a new struts. xml file in the src directory and configure the action class. The contents of the DTD Go To The struts2-core-2.1.8.1.jar to find the struts-default.xml file and copy it from it.Struts. xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="demo" extends="struts-default" namespace="/test"><action name="login" class="action.Login"><result name="success" type="dispatcher">/ok.jsp</result><result name="login" type="redirect">/login.jsp</result></action></package></struts>

6. Write view resources.OK. jsp:

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.