首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

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

2012-11-26 
Struts2 Notes(One)----set up the first sturts2 applicationd1. Overview of Struts2Struts2 is the nex

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:


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


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

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

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

<?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


热点排行
Bad Request.