Framework learning-struts2 Section 1 Development Environment establishment and first application development

Source: Internet
Author: User

1. Introduction to struts2 and its six main advantages

 

2. Build struts2 Development Environment

 

Step 1: Introduce the jar package (only a few major jar packages need to be introduced) and put the following JAR packages under the lib directory of the project.

 

Step 2: Compile the configuration file struts. xml. Put the file in the src directory.

Instance:

<?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="yinger" namespace="/test" extends="struts-default">        <action name="helloworld" class="com.yinger.HelloWorld" method="hello">            <result name="helloworld">/WEB-INF/page/hello.jsp            </result>        </action>    </package>    <!-- Add packages here --></struts>

 

 

Step 3: Write the Web. xml file in the WEB-INF directory

Instance:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     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">  <display-name></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>

Note the struts version: Filter-class is different.

 

Publish and start the Tomcat server. If the struts configuration file is loaded and there is no error, the configuration is successful.

 

3. Development of the first application

Build the development environment according to the above introduction, and write the Web. xml and Struts. XML according to the instance.

The following is an explanation of the Package content in struts. xml:

 

4. Development Process explanation

Step 1: Write wbe. xml and Struts. xml: Same as above

Step 2: Write Java classes and Methods

package com.yinger;public class HelloWorld {    private String msg;        public String getMessage() {        return msg;    }    public String execute(){        msg = "My first struts2 application!";        return "success";    }}

Step 3: Compile JSP and test the result

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

The file directory structure is as follows:

 

Test results:

 

Reference:

How does the Code work?

Your browser sends http: // localhost: 8080/struts/to the Web server/Test/Helloworld (Note: struts is the project name deployed in Tomcat) URL request, the server does the following work:

1. the container receives helloworld from the web server. action request, according to the Web. in XML, the server will contain. the request with the extension of action is forwarded to Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter class. This filterdispatcher is an entry point of the Framework;

2. The framework finds the class corresponding to the action named helloworld In the struts. xml configuration file. The framework initializes the action and executes the execute method of the action class;

3. The execute method puts information into the message variable and returns success. The framework checks the configuration to view the corresponding page when the returned result is successful. The framework tells the container to obtain the returned result page hello. jsp;

4. in hello. after JSP execution, the <s: property value = "message"/> label calls the getmessage method in the action class of helloworld to obtain the message value, the page that combines values is displayed to the user;

In this example, $ {message} is used}

5. A pure HTML page is displayed on your browser.

 

This is the end of section 1. Thank you for reading this article!

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.