1. 什麼是struts2?java
Struts2是一個mvc框架。web
Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.apache
2. Struts2=struts1+webwork。mvc
3. 在struts2以前就已經有不少mvc框架。當時Strtus1是主流的mvc框架。隨着技術發展,struts1不支持新的表示層技術。Strtus1和webwork整合而來struts2。app
Struts2的核心是webwork,是一個輕量級的mvc框架,是一個基於請求的mvc框架。框架
4. 輕量級框架通常佔用資源較少,使用比較方便,不具備侵入性的。jsp
5. 沒有侵入性指使用一個框架,不去繼承或者實現框架提供的類或接口。測試
6. 使用struts2開發一個helloworld程序url
a) 新建web項目spa
b) 添加index.jsp頁面
c) 添加struts2須要的jar包
asm-x.x.jar asm-commons-x.x.jar asm-tree-x.x.jar commons-fileupload-X.X.X.jar commons-io-X.X.X.jar commons-lang-X.X.jar commons-lang3-3.1.jar freemarker-X.X.X.jar javassist-X.X.X.jar ognl-X.X.X.jar struts2-core-X.X.X.X.jar xwork-core-X.X.X.jar (最新的2.5.10.1將xwork包與struts包合併了) |
d) 在web.xml中添加struts2的核心過濾器
<!-- struts2的核心過濾器 --> <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> |
e) 新建一個Action類,該類用於處理請求
public class HelloAction {
public String execute(){ System.out.println("處理action的請求"); return "index"; } } |
f) 在src下建立struts.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default"> <action name="hello" class="cn.sxt.action.HelloAction"> <result name="index">/index.jsp</result> </action> </package> </struts> |
g) 測試:http://localhost/helloworld/hello