Struts2 Framework Study Notes 1: Build a test, struts2 Study Notes

Source: Internet
Author: User

Struts2 Framework Study Notes 1: Build a test, struts2 Study Notes

Servlet is thread-insecure, and Struts1 is a Servlet-based framework.

Struts2 is a Filter-based framework that solves thread security issues.

Therefore, Struts1 and Struts2 are basically irrelevant, but the name of the creator is incorrect.

 

Next, set up and test

Download Struts2: https://struts.apache.org/

The decompressed directory is as follows:

 

Examples in apps, documents in docs, libraries in lib, and source code in src

 

Import the package without importing all the packages in lib.

 

 

Simply write an Action class:

package hello;public class HelloAction {        public String hello(){        System.out.println("hello world!");        return "success";    }    }

 

Import dtd constraints, which can be omitted here, but there is no prompt when writing the configuration file, it is easy to make mistakes by hand

Configuration File: 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="hello" namespace="/hello" extends="struts-default" >        <action name="HelloAction" class="hello.HelloAction" method="hello" >            <result name="success">/hello.jsp</result>        </action>    </package></struts>

 

 

The next step is crucial. Configure web. xml:

Because Struts2 is a Filter-based framework, you must configure web. xml:

<! -- Struts2 core 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>

 

Hello. jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

 

Now the framework has been built!

Deploy the service to the Tomcat server and perform a local test:

 

And print hello world on the console!

Test successful !!

 

Process Analysis:

1. access in the browser.../hello/HelloAction

2. Send the request to the Struts2 filter of web. xml, matching hello and HelloAction

3. The filter searches for the core configuration file, And/hello matches the namespace attribute, so it looks for the HelloAction class.

4. Locate the HelloAction class, create an Action object, call the hello method of the class, print hello world, and return "success"

5. Find the name attribute of the result tag in the returned value. The matching is successful and forwarded to hello. jsp.

6. display the final front-end page

 

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.