Struts2 series notes (1) and struts2 series notes

Source: Internet
Author: User

Struts2 series notes (1) and struts2 series notes

Struts2 framework

If you have always used servlet to retrieve and return data in the MVC mode, now you can learn about the struts2 framework,

Struts is an excellent framework for implementing the MVC design pattern. I will not talk about its many advantages.

I used a self-made figure to illustrate the difference between servlet and struts2.

Write a basic development step to complete development.

(1) create a WEB project

(2) import necessary jar packages

(3) Compile a JSP page

(4) Compile the Action server processing logic

(5) configure the web. xml and struts. xml frameworks.

(6) run the test

(1) create a WEB project

This step is quite normal. Create a common web project.

(2) import necessary jar packages

First, I want to tell you where to download this framework: on the apache official website, you can find it by downloading Baidu struts2.

Introduction to necessary jar packages for Struts running:

To facilitate the import, you can use the app/struts2-blank.war to carry the jar package

(3) Compile a JSP page

Create a Demo file under webRoot. The webpage first accesses start. jsp and then returns to the result page through the framework.

 

Start. jsp interface:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 

Result. jsp Interface

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 

(4) Compile the Action server processing logic

This is to create a class through java. This class can be said to be the most common class of action.

1 package com. yudian. struts; 2 public class HelloAction {3 public String execute () throws Exception {// here execute is used to execute this method by default. 4 System. out. println ("I am executing the content... "); 5 return" excutesuccess "; // return value, which is used to jump to page 6} 7}

(5) configure the web. xml and struts. xml frameworks.

The configuration file is the core key. Configure web. xml first.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "2.5" 3 xmlns = "http://java.sun.com/xml/ns/javaee" 4 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 5 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name> </display-name> 8 <! -- Configure the front-end controller of Struts2 --> <! -- Key in filter-class, fixed --> 9 <filter> 10 <filter-name> struts2 </filter-name> 11 <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> 12 </filter> 13 <filter-mapping> 14 <filter-name> struts2 </filter-name> 15 <url-pattern>/* </url-pattern> 16 </filter-mapping> 17 18 19 <welcome-file-list> 20 <welcome-file> index. jsp </welcome-file> 21 </welcome-file-list> 22 </web-app>

When configuring struts. xml, note that this is placed under src rather than under the package.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE struts PUBLIC 3 "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name = "default" extends = "struts-default"> 8 <! -- 3. Default processing class of Action and Action --> 9 <! -- 1. Get to know Struts2 --> <! -- Here hello must be hello in jsp. Only when the action is consistent can we find --> 10 <action name = "hello" class = "com. yudian. struts. helloAction "> 11 <result name =" excutesuccess ">/demo/result. jsp </result> 12 </action> <! -- Result indicates that if the returned value is excutesuccess, the page is redirected to --> 13 </package> 14 </struts>

(6) run the test

First, access the start interface through a browser:

When you click: access the Struts2 Entry Program connection, the result. jsp is directly redirected.

Running result:

The above is the use of the most basic struts framework, which is very simple and mainly for your understanding. I will gradually add the following to every detail.

Thank you for your comments!

Related 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.