轉載請註明出處:
http://blog.csdn.net/gane_cheng/article/details/52795914
http://www.ganecheng.tech/blog/52795914.html (瀏覽效果更好)
SSH 和 SSM 的定義
本人經歷過兩種技術架構,分別是常說的 SSH 和 SSM ,SSH 在本科的時候老師就教過。SSM 則是去公司後用的比較多。現在我想將這兩大陣營的技術做一下對比。由於本人能力有限,涉及技術較多,我只從具體的應用方面做一些對比。
SSH 通常指的是 Struts2 做前端控制器,spring 管理各層的組件,hibernate 負責持久化層。
SSM 則指的是 SpringMVC 做前端控制器,Spring 管理各層的組件,MyBatis 負責持久化層。
共同之處是都使用了Spring的依賴注入DI來管理各層的組件,使用了面向切面編程AOP來實現日誌管理,權限認證,事務等通用功能的切入。
不同之處是 Struts2 和 SpringMVC 做前端控制器的區別,以及 Hibernate 和 MyBatis 做持久化時的區別。但是,Struts2 也可以和 MyBatis 搭配使用,SpringMVC 也可以和 Hibernate 搭配使用。本文爲了簡化對比,指定 Struts2 要和 Hibernate 搭配,SpringMVC 要和 MyBatis 搭配。
SSH 和 SSM 的實現原理區別
所在分層 | SSH | SSM |
---|---|---|
頁面層(View) | JSP | JSP |
控制器層(Controller) | Struts2 | SpringMVC |
業務層(Service) | Java | Java |
持久層(DAO) | Hibernate | MyBatis |
數據庫層(DB) | MySQL/Oracle | MySQL/Oracle |
組件管理(Bean) | Spring | Spring |
(1) Struts2 的原理
一個請求在Struts2框架中的處理大概分爲以下幾個步驟:
1、客戶端初始化一個指向Servlet容器(例如Tomcat)的請求
2、這個請求經過一系列的過濾器(Filter)(這些過濾器中有一個叫做ActionContextCleanUp的可選過濾器,這個過濾器對於Struts2和其他框架的集成很有幫助,例如:SiteMesh Plugin)
3、接着FilterDispatcher被調用,FilterDispatcher詢問ActionMapper來決定這個請求是否需要調用某個Action
FilterDispatcher是控制器的核心,就是mvc中c控制層的核心。下面粗略的分析下FilterDispatcher工作流程和原理:FilterDispatcher進行初始化並啓用核心doFilter。
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
showDeprecatedWarning();
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
ServletContext servletContext = getServletContext();
String timerKey = "FilterDispatcher_doFilter: ";
try {
// FIXME: this should be refactored better to not duplicate work with the action invocation
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
ActionContext ctx = new ActionContext(stack.getContext());
ActionContext.setContext(ctx);
UtilTimerStack.push(timerKey);
request = prepareDispatcherAndWrapRequest(request, response);
ActionMapping mapping;
try {
//在這裏找到Action的映射器
mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());
} catch (Exception ex) {
log.error("error getting ActionMapping", ex);
dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
return;
}
//沒有此Action的話,就去查找靜態資源
if (mapping == null) {
// there is no action in this request, should we look for a static resource?
String resourcePath = RequestUtils.getServletPath(request);
if ("".equals(resourcePath) && null != request.getPathInfo()) {
resourcePath = request.getPathInfo();
}
if (staticResourceLoader.canHandle(resourcePath)) {
staticResourceLoader.findStaticResource(resourcePath, request, response);
} else {
// this is a normal request, let it pass through
chain.doFilter(request, response);
}
// The framework did its job here
return;
}
//有此Action的話則把控制權交給ActionProxy
dispatcher.serviceAction(request, response, servletContext, mapping);
} finally {
dispatcher.cleanUpRequest(request);
try {
ActionContextCleanUp.cleanUp(req);
} finally {
UtilTimerStack.pop(timerKey);
}
devModeOverride.remove();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
4、如果ActionMapper決定需要調用某個Action,FilterDispatcher把請求的處理交給ActionProxy
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
boolean nullStack = stack == null;
if (nullStack) {
ActionContext ctx = ActionContext.getContext();
if (ctx != null) {
stack = ctx.getValueStack();
}
}
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
}
String timerKey = "Handling request from Dispatcher";
try {
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
//獲取配置文件
Configuration config = configurationManager.getConfiguration();
//根據配置文件找到此Action並生成ActionProxy
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
//ActionProxy創建一個ActionInvocation的實例
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (!nullStack) {
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
logConfigurationException(request, e);
sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
} catch (Exception e) {
if (handleException || devMode) {
sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
} else {
throw new ServletException(e);
}
} finally {
UtilTimerStack.pop(timerKey);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
5、ActionProxy通過Configuration Manager詢問框架的配置文件,找到需要調用的Action類
6、ActionProxy創建一個ActionInvocation的實例。
7、ActionInvocation實例使用命名模式來調用,在調用Action的過程前後,涉及到相關攔截器(Intercepter)的調用。
8、一旦Action執行完畢,ActionInvocation負責根據struts.xml中的配置找到對應的返回結果。返回結果通常是(但不總是,也可 能是另外的一個Action鏈)一個需要被表示的JSP或者FreeMarker的模版。
9、將處理結果返回給客戶端
(2) SpringMVC 的原理
執行步驟:
第一步:發起請求到前端控制器(DispatcherServlet)
第二步:前端控制器請求HandlerMapping查找 Handler
可以根據xml配置、註解進行查找
第三步:處理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器調用處理器適配器去執行Handler
第五步:處理器適配器去執行Handler
第六步:Handler執行完成給適配器返回ModelAndView
第七步:處理器適配器向前端控制器返回ModelAndView
ModelAndView是SpringMVC框架的一個底層對象,包括 Model和view
第八步:前端控制器請求視圖解析器去進行視圖解析
根據邏輯視圖名解析成真正的視圖(jsp)
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進行視圖渲染
視圖渲染將模型數據(在ModelAndView對象中)填充到request域
第十一步:前端控制器向用戶響應結果
(3) Hibernate 的原理
1.通過Configuration().configure();讀取並解析hibernate.cfg.xml配置文件
2.由hibernate.cfg.xml中的<mapping resource="com/xx/User.hbm.xml"/>
讀取並解析映射信息
3.通過config.buildSessionFactory();//創建SessionFactory
4.sessionFactory.openSession();//打開Sesssion
5.session.beginTransaction();//創建事務Transation
6.persistent operate持久化操作
7.session.getTransaction().commit();//提交事務
8.關閉Session
9.關閉SesstionFactory
(4) MyBatis原理
MyBatis框架執行過程:
1、配置MyBatis的配置文件,SqlMapConfig.xml(名稱不固定)
2、通過配置文件,加載MyBatis運行環境,創建SqlSessionFactory會話工廠
SqlSessionFactory 在實際使用時按單例方式。
3、通過SqlSessionFactory創建SqlSession
SqlSession 是一個面向用戶接口(提供操作數據庫方法),實現對象是線程不安全的,建議sqlSession應用場合在方法體內。
4、調用 sqlSession 的方法去操作數據。
如果需要提交事務,需要執行 SqlSession 的 commit() 方法。
5、釋放資源,關閉SqlSession
Struts2 和 SpringMVC 在 web.xml 中配置的不同
(1) Struts2
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>filterConfig</param-name>
<param-value>classpath:struts2/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Struts2使用Filter嵌入自己的框架。配置文件加載順序爲:default.properties -> struts-default.xml -> struts-plugins.xml -> struts.xml -> struts.locale。
加載順序可以參考這篇文章的源碼分析瞭解更多。https://my.oschina.net/gschen/blog/121433
(2) SpringMVC
<!-- springmvc前端控制器,rest配置 -->
<servlet>
<servlet-name>springmvc_rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等) 如果不配置contextConfigLocation,默認加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc_rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
SpringMVC使用Servlet嵌入自己的框架。
(3)web.xml不同之處
SpringMVC的入口是Servlet,而Struts2是Filter(這裏要指出,Filter和Servlet是不同的。以前認爲filter是servlet的一種特殊),這就導致了二者的機制不同,這裏就牽涉到Servlet和Filter的區別了。但是這只是接管用戶請求的兩種不同方式而已,控制權被Struts2和SpringMVC掌握之後,想做什麼事都是可以做到的。
Servlet
servlet是一種運行服務器端的Java應用程序,具有獨立於平臺和協議的特性,並且可以動態的生成web頁面,它工作在客戶端請求與服務器響應的中間層。最早支持 Servlet 技術的是 JavaSoft 的 Java Web Server。此後,一些其它的基於 Java 的 Web Server 開始支持標準的 Servlet API。Servlet 的主要功能在於交互式地瀏覽和修改數據,生成動態 Web 內容。這個過程爲:
1) 客戶端發送請求至服務器端;
2) 服務器將請求信息發送至 Servlet;
3) Servlet 生成響應內容並將其傳給服務器。響應內容動態生成,通常取決於客戶端的請求;
4) 服務器將響應返回給客戶端。
在 Web 應用程序中,一個 Servlet 在一個時刻可能被多個用戶同時訪問。這時 Web 容器將爲每個用戶創建一個線程來執行 Servlet。如果 Servlet 不涉及共享資源的問題,不必關心多線程問題。但如果 Servlet 需要共享資源,需要保證 Servlet 是線程安全的。
爲了簡化開發流程,Servlet 3.0 引入了註解(annotation),這使得 web 部署描述符 web.xml 不再是必須的選擇。
Filter:Filter是一個可以複用的代碼片段,可以用來轉換HTTP請求、響應和頭信息。Filter不像Servlet,它不能產生一個請求或者響應,它只是修改對某一資源的請求,或者修改從某一的響應。Servlet中的過濾器Filter是實現了javax.servlet.Filter接口的服務器端程序,主要的用途是過濾字符編碼、做一些業務邏輯判斷等。其工作原理是,只要你在web.xml文件配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就可以對請求或響應(Request、Response)統一設置編碼,簡化操作;同時還可進行邏輯判斷,如用戶是否已經登陸、有沒有權限訪問該頁面等等工作。它是隨你的web應用啓動而啓動的,只初始化一次,以後就可以攔截相關請求,只有當你的web應用停止或重新部署的時候才銷燬。Filter可認爲是Servlet的一種“變種”,它主要用於對用戶請求進行預處理,也可以對HttpServletResponse進行後處理,是個典型的處理鏈。它與Servlet的區別在於:它不能直接向用戶生成響應。完整的流程是:Filter對用戶請求進行預處理,接着將請求交給Servlet進行處理並生成響應,最後Filter再對服務器響應進行後處理。
Servlet與Filter的區別可以從這篇文章瞭解更多。http://www.cnblogs.com/doit8791/p/4209442.html
Struts2 和 SpringMVC 處理用戶請求的不同
Struts2和SpringMVC的核心都是接管用戶的請求,解決傳統Servlet開發過於繁瑣,重用性不高的問題。
Struts2和SpringMVC都有註解和配置文件兩種匹配用戶請求URL的方式。
Struts2註解方式匹配URL
參考網址:http://struts.apache.org/docs/convention-plugin.html
首先需要將架包(struts2-convention-plugin-xxx.jar)導入工程中
示例
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
@Results({
@Result(name="failure", location="fail.jsp")
})
public class HelloWorld extends ActionSupport
{
@Action(value="/different/url",
results={@Result(name="success", location="http://struts.apache.org", type="redirect")}
)
public String execute()
{
return SUCCESS;
}
@Action("/another/url")
public String doSomething()
{
return SUCCESS;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
Struts2配置方式匹配URL
<package name="package" namespace="/different" extends="struts-default">
<global-results>
<result name="failure">/fail.jsp</result>
</global-results>
<action name="url" class="com.example.actions.HelloWorld" method="execute">
<result name="success" type="redirect">http://struts.apache.org</result>
</action>
</package>
<package name="package2" namespace="/another" extends="struts-default">
<global-results>
<result name="failure">/fail.jsp</result>
</global-results>
<action name="url" class="com.example.actions.HelloWorld" method="doSomething">
</action>
</package>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
SpringMVC註解方式匹配URL
package com.jpkc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("/admin")
@Controller
public class LoginController
{
@RequestMapping("/admin_home")
public String admin_home() throws Exception
{
return "forward:/shop/index.jsp";
}
@RequestMapping("/exit")
public String logout(ModelAndView model, HttpSession session) throws Exception
{
session.invalidate();
return "redirect:/manager/login.jsp";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
SpringMVC配置方式匹配URL
public class ItemsController1 implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
{
//調用Service查找 數據庫,查詢商品列表,這裏使用靜態數據模擬
List<Items> itemsList = new ArrayList<Items>();
//向list中填充靜態數據
Items items_1 = new Items();
items_1.setName("聯想筆記本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
Items items_2 = new Items();
items_2.setName("蘋果手機");
items_2.setPrice(5000f);
items_2.setDetail("iphone6蘋果手機!");
itemsList.add(items_1);
itemsList.add(items_2);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
modelAndView.addObject("itemsList", itemsList);
//指定視圖
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
<!-- 配置Handler -->
<bean id="itemsController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1" />
- 1
- 2
- 1
- 2
1、Struts2是類級別的攔截, 一個類對應一個request上下文,SpringMVC是方法級別的攔截,一個方法對應一個request上下文,而方法同時又跟一個url對應,所以說從架構本身上SpringMVC就容易實現restful url,而struts2的架構實現起來要費勁,因爲Struts2中Action的一個方法可以對應一個url,而其類屬性卻被所有方法共享,這也就無法用註解或其他方式標識其所屬方法了。
2、由上邊原因,SpringMVC的方法之間基本上獨立的,獨享request response數據,請求數據通過參數獲取,處理結果通過ModelMap交回給框架,方法之間不共享變量,而Struts2搞的就比較亂,雖然方法之間也是獨立的,但其所有Action變量是共享的,這不會影響程序運行,卻給我們編碼 讀程序時帶來麻煩,每次來了請求就創建一個Action,一個Action對象對應一個request上下文。
3、由於Struts2需要針對每個request進行封裝,把request,session等servlet生命週期的變量封裝成一個一個Map,供給每個Action使用,並保證線程安全,所以在原則上,是比較耗費內存的。
Struts2 和 SpringMVC 實現 RESTful 的不同
http://localhost/jpkc/item/1609032329404095427579225
實現上面這個鏈接,其中localhost是域名,jpkc是項目名。
Struts2實現方式
<package name="course_info_package" namespace="/item" extends="struts-default">
<action name="*" class="com.jpkc.action.CourseAction" method="get_course_info">
<result name="success">/story/story_02.jsp</result>
</action>
</package>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
public class CourseAction extends ActionSupport
{
public String get_course_info()
{
String actionName = ServletActionContext.getActionMapping().getName();
CourseInfo courseInfoFromDB = courseInfoDAO.findById(actionName);
if (courseInfoFromDB == null)
{
return "404";
}
Course courseFromDB = courseDAO.findById(actionName);
if (courseFromDB == null)
{
return "404";
}
setCourseInfo(courseInfoFromDB);
setCourse(courseFromDB);
return SUCCESS;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
SpringMVC實現方式
@Controller
public class CourseController
{
@RequestMapping("/item/{id}")
public ModelAndView get_course_info(ModelAndView model, @PathVariable("id") String id)
{
if (CM.validIsEmptyWithTrim(id))
{
model.addObject("message", "沒有找到此視頻頁面");
model.setViewName("/WEB-INF/jsp/error");
return model;
}
CourseInfo courseInfoFromDB=null;
try
{
courseInfoFromDB = courseInfoService.selectByPrimaryKey(id);
}
catch (Exception e1)
{
System.out.println("沒有找到課程信息");
}
if (courseInfoFromDB == null)
{
model.addObject("message", "沒有找到此視頻頁面");
model.setViewName("/WEB-INF/jsp/error");
return model;
}
Course courseFromDB = null;
try
{
courseFromDB = courseService.selectByPrimaryKey(id);
}
catch (Exception e)
{
System.out.println("沒有查找到課程");
}
if (courseFromDB == null)
{
model.addObject("message", "沒有找到此視頻頁面");
model.setViewName("/WEB-INF/jsp/error");
return model;
}
model.addObject("courseInfo", courseInfoFromDB);
model.addObject("course", courseFromDB);
model.setViewName("/story/story_02");
return model;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
對於類似於http://localhost/jpkc/item/id1這種鏈接,Struts2實現RESTful風格需要在代碼中調用ServletActionContext.getActionMapping().getName()獲取ActionName。SpringMVC直接將鏈接映射到方法參數裏去了。
如果類似於http://localhost/jpkc/id2/id1這種鏈接,Struts2要進一步分析鏈接得到id1和id2。SpringMVC依然可以將id2映射到方法參數上。從調用的角度來看SpringMVC要方便一些。但是如果將Struts2獲取方式封裝一下,也可以得到同樣的效果。
Struts2 和 SpringMVC 獲取 request 參數的不同
前臺頁面有一個表單需要提交。
Struts2 接收 request 參數
<form class="login-form" action="/login_do" method="post">
<h3 class="form-title">登錄系統</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> 請輸入用戶名和密碼 </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">用戶名</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="用戶名" name="account.id" />
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">密碼</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="密碼" name="account.password" />
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn green pull-right">
登錄 <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
package com.jpkc.pojo;
import java.io.Serializable;
public class Account implements Serializable
{
private String id;
private String password;
private String name;
public Account()
{
super();
// TODO Auto-generated constructor stub
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
package com.jpkc.action;
import java.util.HashMap;
import java.util.Map;
import com.jpkc.common.CM;
import com.jpkc.pojo.Account;
public class AccountAction extends BaseAction
{
private Account account;
public String login_do()
{
String method = getRequest().getMethod();
if (method.toUpperCase().equals("GET"))
{
return "404";
}
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
return ERROR;
}
getSession().setAttribute("accountSession", account);
return SUCCESS;
}
public Account getAccount()
{
return account;
}
public void setAccount(Account account)
{
this.account = account;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
SpringMVC 接收 request 參數
<form class="login-form" action="admin/login_do" method="post">
<h3 class="form-title">登錄系統</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> 請輸入用戶名和密碼 </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">用戶名</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="用戶名" name="id" />
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">密碼</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="密碼" name="password" />
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn green pull-right">
登錄 <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
package com.jpkc.pojo;
import java.io.Serializable;
public class Account implements Serializable
{
private String id;
private String password;
private String name;
public Account()
{
super();
// TODO Auto-generated constructor stub
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
package com.jpkc.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.jpkc.common.CM;
import com.jpkc.exception.CustomException;
import com.jpkc.mapper.CourseInfoMapper;
import com.jpkc.pojo.Account;
import com.jpkc.pojo.CourseInfo;
import com.jpkc.service.LoginService;
@RequestMapping("/admin")
@Controller
public class LoginController
{
@Autowired
LoginService loginService;
@RequestMapping(value = "/login_do", method = { RequestMethod.POST })
public void login_do(ModelAndView model, HttpServletRequest request, HttpServletResponse response, HttpSession session, Account account) throws Exception
{
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
Map<String, Object> json = new HashMap<String, Object>();
String info;
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
info = "用戶名、密碼都是必填項。";
json.put("success", false);
json.put("info", info);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
session.setAttribute("accountSession", account);
json.put("success", true);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
Struts2單個方法可以處理一個request,接收參數Account需要定義一個成員變量,Struts2會自動將對應的參數調用成員變量的set方法設置進去。處理方法可以在方法內獲取到。用完還存在request級別Map中。
SpringMVC的單個方法也對應於一個request,接收參數Account需要定義一個方法參數,SpringMVC會自動將對應的參數設置到方法參數中去。處理方法可以在方法內獲取到。用完即銷燬。
可以看出兩種框架都可以實現參數的自動轉換。Struts2定義一個成員變量,其他方法都是可以共享的,不用重新定義。SpringMVC每個方法都是獨立的,方法參數是每一個方法獨享的。
各有利弊。
成員變量共享可以避免重複定義,但是方法一多,用到的成員變量原來越多,整個Action類會慘不忍睹,因爲你不知道其中一個方法具體會用到哪幾個成員變量。而且用不到的成員變量也被存儲到request級別Map中了。造成內存的浪費。
方法參數是方法獨享的。則不能複用到其他方法,但是對於當前方法來說有哪些參數足夠明確,而且不用和其他方法攪合,乾脆利落。
從JVM角度來說,Struts2成員變量會被分配到堆中。SpringMVC方法參數則會存在於方法棧中,一般認爲棧比堆更輕量一些,方法結束,用完參數即回收。堆需要垃圾回收觸發時才能統一回收。
Struts2 和 SpringMVC 限制訪問方式GET和POST的不同
在上例中,表單提交有密碼,需要指定只接受POST提交方式。
Struts2指定POST方式
public String login_do()
{
String method = getRequest().getMethod();
if (method.toUpperCase().equals("GET"))
{
return "404";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
SpringMVC指定POST方式
@RequestMapping(value = "/login_do", method = { RequestMethod.POST })
public void login_do(ModelAndView model, HttpServletRequest request, HttpServletResponse response, HttpSession session, Account account) throws Exception
{
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
Map<String, Object> json = new HashMap<String, Object>();
String info;
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
info = "用戶名、密碼都是必填項。";
json.put("success", false);
json.put("info", info);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
session.setAttribute("accountSession", account);
json.put("success", true);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
Struts2限制只能通過POST方式訪問,是通過調用request的getMethod方法來得到當前訪問方式。然後手工的去判斷。
SpringMVC也可以調用request的getMethod方法來判斷,但是框架本身提供了方便的內置判斷。使用註解即可。
Struts2通過攔截器設置好訪問方式的代碼後,也可以通過註解的方式指定攔截器得到同樣的效果。本身不是太難的事情,兩個框架都可以實現,Struts2需要手工實現,SpringMVC默認提供了。即使SpringMVC不提供,調用SpringMVC的攔截器也能和Struts2的攔截器的效果一樣。在GET和POST訪問限制方面,並沒有誰優誰劣,都可以實現。只是SpringMVC願意往前多走一小步。
Struts2 和 SpringMVC 攔截器的不同
後臺頁面需要登錄,我們可以使用攔截器限制未登錄的用戶訪問。
Struts2實現攔截器的方式
public class ManagerLoginInterceptor extends AbstractInterceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
String actionName = ServletActionContext.getActionMapping().getName();
// 如果是登錄、註冊、退出的話就不要攔截了
if (actionName.equals("exit") || actionName.equals("login") || actionName.equals("login_do") || actionName.equals("regist")
|| actionName.equals("regist_do"))
{
return invocation.invoke();
}
// 如果不是管理員就不能進入
Manager managerTemp = (Manager) ServletActionContext.getRequest().getSession().getAttribute("managerSession");
if (managerTemp == null)
{
return "manager_login";
}
//驗證成功,放行。
return invocation.invoke();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
<package name="admin_package" namespace="/admin" extends="ssh-default">
<interceptors>
<interceptor name="LoginManagerValidate" class="com.example.interceptor.ManagerLoginInterceptor">
</interceptor>
<!-- 自定義攔截器棧-攔截未登錄的管理員- -->
<interceptor-stack name="LoginManagerValidateStack">
<interceptor-ref name="LoginManagerValidate"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="m_*" class="com.example.action.ManagerAction" method="m_{1}">
<interceptor-ref name="LoginManagerValidateStack"></interceptor-ref>
<result name="success" type="json">
<param name="root">json</param>
</result>
</action>
</package>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Struts2還提供了很多默認的攔截器供用戶調用。
<interceptors>
<interceptor name="alias"class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring"class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain"class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError"class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="clearSession"class="org.apache.struts2.interceptor.ClearSessionInterceptor"/>
<interceptor name="createSession"class="org.apache.struts2.interceptor.CreateSessionInterceptor"/>
<interceptor name="debugging"class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor"/>
<interceptor name="externalRef"class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>
<interceptor name="execAndWait"class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
<interceptor name="exception"class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
<interceptor name="fileUpload"class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor name="i18n"class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
<interceptor name="logger"class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
<interceptor name="modelDriven"class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor name="scopedModelDriven"class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
<interceptor name="params"class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
<interceptor name="actionMappingParams"class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
<interceptor name="prepare"class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="staticParams"class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
<interceptor name="scope"class="org.apache.struts2.interceptor.ScopeInterceptor"/>
<interceptor name="servletConfig"class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="sessionAutowiring"class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
<interceptor name="timer"class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
<interceptor name="token"class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession"class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation"class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
<interceptor name="workflow"class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="store"class="org.apache.struts2.interceptor.MessageStoreInterceptor"/>
<interceptor name="checkbox"class="org.apache.struts2.interceptor.CheckboxInterceptor"/>
<interceptor name="profiling"class="org.apache.struts2.interceptor.ProfilingActivationInterceptor"/>
<interceptor name="roles"class="org.apache.struts2.interceptor.RolesInterceptor"/>
<interceptor name="jsonValidation"class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor"/>
<interceptornameinterceptorname="annotationWorkflow"class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
SpringMVC實現攔截器的方式
public class LoginInterceptor implements HandlerInterceptor
{
// 進入 Handler方法之前執行
// 用於身份認證、身份授權
// 比如身份認證,如果認證通過表示當前用戶沒有登陸,需要此方法攔截不再向下執行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
// 獲取請求的url
String url = request.getRequestURI();
// 判斷url是否是公開 地址(實際使用時將公開 地址配置配置文件中)
// 這裏公開地址是登陸提交的地址
if (url.indexOf("login") >= 0 || url.indexOf("exit") >= 0)
{
// 如果進行登陸提交,放行
return true;
}
// 判斷session
HttpSession session = request.getSession();
// 從session中取出用戶身份信息
Account account = (Account) session.getAttribute("accountSession");
if (account != null)
{
// 身份存在,放行
return true;
}
// 執行這裏表示用戶身份需要認證,跳轉登陸頁面
request.getRequestDispatcher("/manager/login.jsp").forward(request, response);
// return false表示攔截,不向下執行
// return true表示放行
return false;
}
// 進入Handler方法之後,返回modelAndView之前執行
// 應用場景從modelAndView出發:將公用的模型數據(比如菜單導航)在這裏傳到視圖,也可以在這裏統一指定視圖
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
{
System.out.println("HandlerInterceptor1...postHandle");
}
// 執行Handler完成執行此方法
// 應用場景:統一異常處理,統一日誌處理
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
{
System.out.println("HandlerInterceptor1...afterCompletion");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
<!--攔截器 -->
<mvc:interceptors>
<!--多個攔截器,順序執行 -->
<!-- 登錄認證攔截器 -->
<mvc:interceptor>
<mvc:mapping path="/admin/**" />
<bean class="com.jpkc.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
攔截器實現機制上,Struts2有自己的interceptor機制,SpringMVC用的是獨立的AOP方式。都可以實現在前後進行攔截。
Struts2 和 SpringMVC 支持 JSON 的不同
有時我們界面的一些操作,是通過 Ajax 調用後臺的服務,獲取服務器返回的 json 數據,進行後續的操作。
Struts2 實現JSON數據返回的方式
<action name="login_do" class="com.jpkc.action.AccountAction" method="login_do">
<result name="success" type="json">
<!-- 這裏指定將被Struts2序列化的屬性,該屬性在action中必須有對應的getter方法 -->
<param name="root">json</param>
</result>
</action>
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
public class AccountAction extends BaseAction
{
// 常用變量
private Map<String, Object> json;// 返回到前臺的map對象
private Account account;
public AccountAction()
{
json = new HashMap<String, Object>();
}
public String login_do()
{
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
info = "用戶名、密碼都是必填項。";
json.put("success", false);
json.put("info", info);
return SUCCESS;
}
getSession().setAttribute("accountSession", account);
json.put("success", true);
return SUCCESS;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
$.post(<span class="hljs-string">"login_do"</span>, $(".login-form").serialize(), function(json)
{
if (json.success == true)
{
window.location.href="shop/index.jsp";
}
else
{
alert("操作失敗:" + json.info);
}
}, "json");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
SpringMVC 實現JSON數據返回的方式
<!--註解適配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
$.post(<span class="hljs-string">"login_do"</span>, $(".login-form").serialize(), function(json)
{
if (json.success == true)
{
window.location.href="shop/index.jsp";
}
else
{
alert("操作失敗:" + json.info);
}
}, "json");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
SpringMVC在控制器中返回json有兩種方式。
一種是使用response返回json。
@RequestMapping(value = "/login_do", method = { RequestMethod.POST })
public void login_do(ModelAndView model, HttpServletRequest request, HttpServletResponse response, HttpSession session, Account account) throws Exception
{
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
Map<String, Object> json = new HashMap<String, Object>();
String info;
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
info = "用戶名、密碼都是必填項。";
json.put("success", false);
json.put("info", info);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
session.setAttribute("accountSession", account);
json.put("success", true);
response.getWriter().write(new ObjectMapper().writeValueAsString(json));
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
另一種是使用@ResponseBody註解方式。
@RequestMapping(value = "/login_do", method = { RequestMethod.POST })
public @ResponseBody Map<String, Object> login_do(ModelAndView model, HttpServletRequest request, HttpServletResponse response, HttpSession session, Account account) throws Exception
{
Map<String, Object> json = new HashMap<String, Object>();
String info;
if (account == null || CM.validIsEmpty(account.getId()) || CM.validIsEmpty(account.getPassword()))
{
info = "用戶名、密碼都是必填項。";
json.put("success", false);
json.put("info", info);
return json;
}
session.setAttribute("accountSession", account);
json.put("success", true);
return json;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
可以看出,Struts2 和 SpringMVC 都可以實現 Ajax 請求返回 JSON。實現方式上,Struts2在配置文件配置返回類型爲JSON。SpringMVC在方法上加一個@ResponseBody註解即可返回對應類型轉成的JSON字符串。都是對返回數據轉成JSON,但是不得不說SpringMVC的寫法方便太多了。
Hibernate 和 MyBatis 在 ORM 側重點的不同
Hibernate對數據庫結構提供了較爲完整的封裝,Hibernate的O/R Mapping實現了POJO 和數據庫表之間的映射,以及SQL 的自動生成和執行。程序員往往只需定義好了POJO 到數據庫表的映射關係,即可通過Hibernate 提供的方法完成持久層操作。程序員甚至不需要對SQL 的熟練掌握, Hibernate/OJB 會根據指定的存儲邏輯,自動生成對應的SQL 並調用JDBC 接口加以執行。
MyBatis 的着力點,則在於POJO 與SQL之間的映射關係。然後通過映射配置文件,將SQL所需的參數,以及返回的結果字段映射到指定POJO。 相對Hibernate“O/R”而言,MyBatis 是一種“Sql Mapping”的ORM實現。
SQL語句支持:Hibernate可以完全不用手寫SQL語句,MyBatis手動維護SQL語句。Hibernate修改優化SQL語句困難,MyBatis由於SQL語句自己控制,優化非常方便。
開發速度:Hibernate的真正掌握要比Mybatis來得難些。Mybatis框架相對簡單很容易上手,但也相對簡陋些。
開發社區:Hibernate 與Mybatis都是流行的持久層開發框架,但Hibernate開發社區相對多熱鬧些,支持的工具也多,更新也快。而Mybatis相對平靜,工具較少。
開發工作量:Hibernate和MyBatis都有相應的代碼生成工具。可以生成簡單基本的DAO層方法。
針對高級查詢,Mybatis需要手動編寫SQL語句,以及ResultMap。而Hibernate有良好的映射機制,開發者無需關心SQL的生成與結果映射,可以更專注於業務流程。
Hibernate 和 MyBatis 在調優方面的不同
- 制定合理的緩存策略;
- 儘量使用延遲加載特性;
- 採用合理的Session管理機制;
SQL優化方面
Hibernate的查詢會將表中的所有字段查詢出來,這一點會有性能消耗。Hibernate也可以自己寫SQL來指定需要查詢的字段,但這樣就破壞了Hibernate開發的簡潔性。而Mybatis的SQL是手動編寫的,所以可以按需求指定查詢的字段。
Hibernate HQL語句的調優需要將SQL打印出來,而Hibernate的SQL被很多人嫌棄因爲太醜了。MyBatis的SQL是自己手動寫的所以調整方便。但Hibernate具有自己的日誌統計。Mybatis本身不帶日誌統計,使用Log4j進行日誌記錄。
擴展性方面
Hibernate與具體數據庫的關聯只需在XML文件中配置即可,所有的HQL語句與具體使用的數據庫無關,移植性很好。MyBatis項目中所有的SQL語句都是依賴所用的數據庫的,所以不同數據庫類型的支持不好。
Hibernate 和 MyBatis 在對象管理與抓取策略的不同
對象管理
Hibernate 是完整的對象/關係映射解決方案,它提供了對象狀態管理(state management)的功能,使開發者不再需要理會底層數據庫系統的細節。也就是說,相對於常見的 JDBC/SQL 持久層方案中需要管理 SQL 語句,Hibernate採用了更自然的面向對象的視角來持久化 Java 應用中的數據。
換句話說,使用 Hibernate 的開發者應該總是關注對象的狀態(state),不必考慮 SQL 語句的執行。這部分細節已經由 Hibernate 掌管妥當,只有開發者在進行系統性能調優的時候才需要進行了解。
而MyBatis在這一塊沒有文檔說明,用戶需要對對象自己進行詳細的管理。當調用sqlSession.commit()方法時纔會進行真正的提交。
抓取策略
Hibernate對實體關聯對象的抓取有着良好的機制。對於每一個關聯關係都可以詳細地設置是否延遲加載,並且提供關聯抓取、查詢抓取、子查詢抓取、批量抓取四種模式。 它是詳細配置和處理的。
而Mybatis的延遲加載是全局配置的,在resultMap中使用association中的select指定延遲加載去執行的statement的id。
<!-- 延遲加載的resultMap -->
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserLazyLoadingResultMap">
<!--對訂單信息進行映射配置 -->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<association property="user" javaType="cn.itcast.mybatis.po.User"
select="cn.itcast.mybatis.mapper.UserMapper.findUserById" column="user_id">
<!-- 實現對用戶信息進行延遲加載 -->
</association>
</resultMap>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Hibernate 和 MyBatis 在緩存機制的不同
Hibernate緩存
Hibernate一級緩存是Session緩存,利用好一級緩存就需要對Session的生命週期進行管理好。建議在一個Action操作中使用一個Session。一級緩存需要對Session進行嚴格管理。
Hibernate二級緩存是SessionFactory級的緩存。 SessionFactory的緩存分爲內置緩存和外置緩存。內置緩存中存放的是SessionFactory對象的一些集合屬性包含的數據(映射元素據及預定SQL語句等),對於應用程序來說,它是隻讀的。外置緩存中存放的是數據庫數據的副本,其作用和一級緩存類似.二級緩存除了以內存作爲存儲介質外,還可以選用硬盤等外部存儲設備。二級緩存稱爲進程級緩存或SessionFactory級緩存,它可以被所有session共享,它的生命週期伴隨着SessionFactory的生命週期存在和消亡。
MyBatis緩存
MyBatis 包含一個非常強大的查詢緩存特性,它可以非常方便地配置和定製。MyBatis 3 中的緩存實現的很多改進都已經實現了,使得它更加強大而且易於配置。
一級緩存是SqlSession級別的緩存,二級緩存是mapper(命名空間)級別的緩存,默認情況下是沒有開啓二級緩存的。
要開啓二級緩存,你需要在你的 SQL 映射文件中添加一行: <cache/>
字面上看就是這樣。這個簡單語句的效果如下:
映射語句文件中的所有 select 語句將會被緩存。
映射語句文件中的所有 insert,update 和 delete 語句會刷新緩存。
緩存會使用 Least Recently Used(LRU,最近最少使用的)算法來收回。
根據時間表(比如 no Flush Interval,沒有刷新間隔), 緩存不會以任何時間順序 來刷新。
緩存會存儲列表集合或對象(無論查詢方法返回什麼)的 1024 個引用。
緩存會被視爲是 read/write(可讀/可寫)的緩存,意味着對象檢索不是共享的,而 且可以安全地被調用者修改,而不干擾其他調用者或線程所做的潛在修改。
所有的這些屬性都可以通過緩存元素的屬性來修改。
比如: <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
這個更高級的配置創建了一個 FIFO 緩存,並每隔 60 秒刷新,存數結果對象或列表的 512 個引用,而且返回的對象被認爲是隻讀的,因此在不同線程中的調用者之間修改它們會 導致衝突。可用的收回策略有, 默認的是 LRU:
LRU – 最近最少使用的:移除最長時間不被使用的對象。
FIFO – 先進先出:按對象進入緩存的順序來移除它們。
SOFT – 軟引用:移除基於垃圾回收器狀態和軟引用規則的對象。
WEAK – 弱引用:更積極地移除基於垃圾收集器狀態和弱引用規則的對象。
flushInterval(刷新間隔)可以被設置爲任意的正整數,而且它們代表一個合理的毫秒 形式的時間段。默認情況是不設置,也就是沒有刷新間隔,緩存僅僅調用語句時刷新。
size(引用數目)可以被設置爲任意正整數,要記住你緩存的對象數目和你運行環境的 可用內存資源數目。默認值是1024。
readOnly(只讀)屬性可以被設置爲 true 或 false。只讀的緩存會給所有調用者返回緩 存對象的相同實例。因此這些對象不能被修改。這提供了很重要的性能優勢。可讀寫的緩存 會返回緩存對象的拷貝(通過序列化) 。這會慢一些,但是安全,因此默認是 false。
相同點
Hibernate和Mybatis的二級緩存除了採用系統默認的緩存機制外,都可以通過實現你自己的緩存或爲其他第三方緩存方案,創建適配器來完全覆蓋緩存行爲。
不同點
Hibernate的二級緩存配置在SessionFactory生成的配置文件中進行詳細配置,然後再在具體的表-對象映射中配置是那種緩存。
MyBatis的二級緩存配置都是在每個具體的表-對象映射中進行詳細配置,這樣針對不同的表可以自定義不同的緩存機制。並且Mybatis可以在命名空間中共享相同的緩存配置和實例,通過Cache-ref來實現。
兩者比較
因爲Hibernate對查詢對象有着良好的管理機制,用戶無需關心SQL。所以在使用二級緩存時如果出現髒數據,系統會報出錯誤並提示。
而MyBatis在這一方面,使用二級緩存時需要特別小心。如果不能完全確定數據更新操作的波及範圍,避免Cache的盲目使用。否則,髒數據的出現會給系統的正常運行帶來很大的隱患。
Hibernate 和 MyBatis 對比總結
兩者相同點
Hibernate與MyBatis都可以是通過SessionFactoryBuider由XML配置文件生成SessionFactory,然後由SessionFactory 生成Session,最後由Session來開啓執行事務和SQL語句。其中SessionFactoryBuider,SessionFactory,Session的生命週期都是差不多的。
Hibernate和MyBatis都支持JDBC和JTA事務處理。
Mybatis優勢
MyBatis可以進行更爲細緻的SQL優化,可以減少查詢字段。
MyBatis容易掌握,而Hibernate門檻較高。
Hibernate優勢
Hibernate的DAO層開發比MyBatis簡單,Mybatis需要維護SQL和結果映射。
Hibernate對對象的維護和緩存要比MyBatis好,對增刪改查的對象的維護要方便。
Hibernate數據庫移植性很好,MyBatis的數據庫移植性不好,不同的數據庫需要寫不同SQL。
Hibernate有更好的二級緩存機制,可以使用第三方緩存。MyBatis本身提供的緩存機制不佳,更新操作不能指定刷新指定記錄,會清空整個表,但是也可以使用第三方緩存。
Hibernate 封裝性好,屏蔽了數據庫差異,自動生成SQL語句,應對數據庫變化能力較弱,SQL語句優化困難。
MyBatis僅實現了SQL語句和對象的映射,需要針對具體的數據庫寫SQL語句,應對數據庫變化能力較強,SQL語句優化較爲方便。
SSH 和 SSM 對比總結
SSH 和 SSM 的技術框架的不同只需要比較Struts2和SpringMVC的不同,以及Hibernate和MyBatis的不同。
對於不同的功能,兩大技術陣營均有對應的解決方案。SSH將配置文件開發用到極致。SSM將註解開發用到極致。
企業進行技術選型,以低成本高回報作爲技術選型的原則,根據項目組的技術力量來進行選擇。
小弟水平有限,只能總結到這裏。更進一步的底層代碼級別的對比,纔是本質的區別。用法上的區別只是表象而已,但是對於廣大開發者來說,誰的開發者用戶體驗好,顯然更能贏得開發者的青睞。
我在GitHub上上傳了同一個項目分別用SSH和SSM開發的例子。僅供參考。https://github.com/ganecheng/SSH_SSM
這裏有一份Java工具和技術的調查,可以參考一下,http://f.dataguru.cn/article-9978-1.html
參考文獻
http://blog.csdn.net/firejuly/article/details/8190229
http://blog.csdn.net/chenyi0834/article/details/7334963
http://blog.csdn.net/gane_cheng/article/details/52787040
http://blog.csdn.net/gane_cheng/article/details/52759099