当前位置:编程学习 > JAVA >>

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

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 many warnings when starting Tomcat. As we can see here, xwork-core-2.1.6.jar is the core jar file of WebWork, these are some clues that sturts2 comes from WebWork
Step3: 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.
step4:  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 many times for actions in the same root, it is used as part of the url to send requests by user

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,