"Java EE Learning Day 34th" "Struts2 study first day"

Source: Internet
Author: User

I. Introduction of STRUTS2

STRUTS2 is a framework for developing MVC applications. It provides solutions to some common problems in the development of Web applications.

1.STRUTS2 scope: The first layer in the three-tier architecture, which is equivalent to the MVC layer, has the effect of standardizing MVC and allowing programmers to develop programs under a certain standard.

2.struts2 's Download: http://struts.apache.org/download.cgi#struts2324

Jar package, source code, documentation readily.

3. History: Struts1 is the world's first MVC framework, released in 2001; Struts2 is not a Struts1 upgrade, developed by WebWork, absorbing the virtues of Struts1 and webwork.

4.struts2 can solve the problem:

(1) Start of page navigation activity

(2) Verification of the legality of user input data

(3) Unified layout

(4) Scalability

(5) Internationalization and localization

(6) Ajax support

(7) Solve the problem of duplicate submission of form

......

5.struts2 work principle: write here suddenly think of Wang Jian teacher, before Wang Jian teacher taught us things suddenly found and struts2 extremely similar, really thank Wang Jian teacher. Do not repeat. See the previous Baseservlet principle.

6.STRUTS2 's first small project: hello,world!

(1) The basic packages that should be added include: Asm-3.3.jar, Asm-commons-3.3.jar, Asm-tree-3.3.jar, Commons-fileupload-1.3.1.jar, Commons-io-2.2.jar , Commons-lang-2.4.jar, Commons-lang3-3.2.jar, Commons-logging-1.1.3.jar, Freemarker-2.3.22.jar, Javassist-3.11.0.ga.jar, Ognl-3.0.6.jar, Struts2-core-2.3.24.jar, Xwork-core-2.3.24.jar a total of 13 bags.

(2) Modify the index.jsp page:

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%>2 <%@ taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C" %>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">4 <HTML>5   <Head>6     <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">7     <title>Insert Title here!</title>8   </Head>9   Ten   <Body> One       <ahref= "<c:url value= '/myaction/helloworldaction.action '/>">Hello,world</a>   A   </Body> - </HTML>
index.jsp

(3) New class: Helloworldaction, this class implements the Com.opensymphony.xwork2.Action interface.

1 /**2  * 3  */4 /**5 * @author Kdyzm6  *7  */8 Package com.kdyzm.action;9 Ten import com.opensymphony.xwork2.Action; One  A Public class Helloworldaction implements Action - { - @Override the Public String Execute () throws Exception { - System.out.println ("hello,world!"); - return "Success"; -     } +      -}
Helloworldaction.java

(4) New file Struts.xml in src directory (file name and location cannot be changed)

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <!DOCTYPE Struts public3 "-//apache software foundation//dtd Struts Configuration 2.3//en"4 "Http://struts.apache.org/dtds/struts-2.3.dtd">5 <Struts>6     <!--the writing of namespace must be added/ -7     < Packagename= "Firstpackage"namespace= "/myaction"extends= "Struts-default">8         <Actionname= "Helloworldaction"class= "Com.kdyzm.action.HelloWorldAction">9             <resultname= "Success">/information/success.jsp</result>Ten         </Action> One     </ Package> A </Struts>
Struts.xml

(5) Configuring filters

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">3     <Filter>4         <Filter-name>Strutsfilter</Filter-name>5         <Filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</Filter-class>6     </Filter>7     <filter-mapping>8         <Filter-name>Strutsfilter</Filter-name>9         <Url-pattern>*.action</Url-pattern>Ten     </filter-mapping> One </Web-app>
Web. XML

(6) test

      

Request Result:

      

(7) Struts.xml configuration file Detailed:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <!DOCTYPE Struts public3 "-//apache software foundation//dtd Struts Configuration 2.3//en"4 "Http://struts.apache.org/dtds/struts-2.3.dtd">5 <Struts>6     <!--the writing of namespace must be added/ -7     < Packagename= "Firstpackage"namespace= "/myaction"extends= "Struts-default">8         <Actionname= "Helloworldaction"class= "Com.kdyzm.action.HelloWorldAction">9             <resultname= "Success">/information/success.jsp</result>Ten         </Action> One     </ Package> A </Struts>

<! Doctype> tag node is a specification to follow, here is a DTD file, we need to follow the DTD standard in the Struts.xml file to be modified!

The struts tag is the root node tag;

The package tag is a packet label, its Name property is customized, but its namespace property is a very important property. The property must be exactly the same as defined by the a tag in index.jsp; extends indicates which package inherits, here is the default package Struts-default, which is a very important package, without which the meaning of struts2 is not used.

The action tag corresponds to a method for processing a JSP page request, the default method is the Execute () method of the action interface, and the class property indicates the path of the classes used.

The result tag corresponds to the results returned after implementing the class invocation method of the action interface, and the Name property must match the return result, where there can be multiple result labels to handle different processing results The text node in the result tag marks a page path that needs to be forwarded or redirected to a page. The default method is to forward. These can be viewed in the underlying source code to see.

(8) Little Practice source code: Https://github.com/kdyzm/day34_struts_HelloWorld

Two

"Java EE Learning Day 34th" "Struts2 study first day"

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.