Struts2 Notes (One) ---- set up the first sturts2 application

Source: Internet
Author: User

D1. Overview of Struts2
Struts2 is the next generation of Struts1, but the difference between Struts2 and Struts1 is not as small as their names. struts2 is greatly different from Struts1, for it is based on WebWork (another famous MVC framework) and uses the central Filter to catch the request and dispatch it to corresponding actions, the corresponding actions will then call the java classes to do corresponding work and pass the result back to the action. the central Filter will then forward a jsp page according to the result to the user.
Here we have the jsp pages as the View level same as in Struts1. And we have Central Filter and user defined actions as the Control level, which is different from Struts1. In Struts1, we use ActionServlet to get all the request to dispatches to corresponding actions. the user defined actions have to extend HttpServlet class which the data reuseability very bad. all these actions can only be used in Struts1 framework. not like Struts1, all the user defined actions are "POJO", they are not reliable ot any outside APIs and make it easy to reuse them. also in the Modle Level Struts1 and Struts2 are the same, actions all the java class to do low level calculation. shown as the following picture:

 

2. set up sturts2
Before we develop our web using Struts2, we have to download it, Struts2 is the product of Apache. we can go to this website: http://struts.apache.org /. at his time being, the newest version of Struts2 is 2.3.7. other software we need are: Tomcat and Eclipse-EE, JDK 7.0 they are all avaliable from internet and opensource.
Step 1: Open Eclipse EE, and create a dynamic web project
Step 2: import sturts2's jar files into the lib folder under WEB-INF, shown as picture

We need only import these jar file, if we import all the jar files of struts2, we may face your warnings when starting Tomcat. as we can see here, the xwork-core-2.1.6.jar is the core jar file of WebWork, these are some clues that sturts2 comes from WebWork
Step 3: config web. xml
[Html]
<? 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>

<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

When starting the project, the filter will use its init () method to read the struts. xml file in the default root. then all the information in the struts. xml will be stored in the main memory as javabean. latter, every time ccomes a customer request, the project need only to read the info in the memory instead of reading struts. xml file.
The tag <filter> and <filter-mapping> set the location of the central filter. so the system will the central filter is and use it to catch all the requests and dispartches them to corresponding actions.
Step 4: config struts. xml file
Different from Struts1, now we have to create a struts. xml file under src folder of our web project.
[Html]
<? 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 = "test" namespace = "/test" extends = "struts-default">
<Action name = "helloworld" class = "cn. itcast. action. HelloWorldAction" method = "execute">
<Result name = "success">/WEB-INF/page/Hello. jsp </result>
</Action>

</Package>
</Struts>
Package: as we can see here, in struts2, package is used to manage all the actions, it can put the actions that with the same function into one group.
Namespace: used to identify the root of the action, so we don't need ot type when times for actions in the same root, it is used as part of the url to send requests by user

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.