免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
基于注解的 Spring MVC 簡(jiǎn)單入門(mén)

以下內(nèi)容是經(jīng)過(guò)自己整理資料、官方文檔所得:


web.xml 配置:

 

<servlet>	<servlet-name>dispatcher</servlet-name>	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>	<init-param>		<description>加載/WEB-INF/spring-mvc/目錄下的所有XML作為Spring MVC的配置文件</description>		<param-name>contextConfigLocation</param-name>		<param-value>/WEB-INF/spring-mvc/*.xml</param-value>	</init-param>	<load-on-startup>1</load-on-startup></servlet><servlet-mapping>	<servlet-name>dispatcher</servlet-name>	<url-pattern>*.htm</url-pattern></servlet-mapping>

 

這樣,所有的.htm的請(qǐng)求,都會(huì)被DispatcherServlet處理;

初始化 DispatcherServlet 時(shí),該框架在 web 應(yīng)用程序WEB-INF 目錄中尋找一個(gè)名為[servlet-名稱]-servlet.xml的文件,并在那里定義相關(guān)的Beans,重寫(xiě)在全局中定義的任何Beans,像上面的web.xml中的代碼,對(duì)應(yīng)的是dispatcher-servlet.xml;當(dāng)然也可以使用<init-param>元素,手動(dòng)指定配置文件的路徑;

dispatcher-servlet.xml 配置:

 

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans			http://www.springframework.org/schema/beans/spring-beans-3.0.xsd			http://www.springframework.org/schema/context 			http://www.springframework.org/schema/context/spring-context-3.0.xsd			http://www.springframework.org/schema/aop 			http://www.springframework.org/schema/aop/spring-aop-3.0.xsd			http://www.springframework.org/schema/tx 			http://www.springframework.org/schema/tx/spring-tx-3.0.xsd			http://www.springframework.org/schema/mvc 			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd			http://www.springframework.org/schema/context 			http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!--        使Spring支持自動(dòng)檢測(cè)組件,如注解的Controller    -->    <context:component-scan base-package="com.minx.crm.web.controller"/>       <bean id="viewResolver"          class="org.springframework.web.servlet.view.InternalResourceViewResolver"          p:prefix="/WEB-INF/jsp/"          p:suffix=".jsp" /></beans>

 

第一個(gè)Controller

package com.minx.crm.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController {    @RequestMapping("/index")    public String index() {        return "index";    }}

@Controller注解標(biāo)識(shí)一個(gè)控制器,@RequestMapping注解標(biāo)記一個(gè)訪問(wèn)的路徑(/index.htm),return "index"標(biāo)記返回視圖(index.jsp);

注:如果@RequestMapping注解在類級(jí)別上,則表示一相對(duì)路徑,在方法級(jí)別上,則標(biāo)記訪問(wèn)的路徑;

@RequestMapping注解標(biāo)記的訪問(wèn)路徑中獲取參數(shù):

Spring MVC 支持RESTful風(fēng)格的URL參數(shù),如:

@Controllerpublic class IndexController {    @RequestMapping("/index/{username}")    public String index(@PathVariable("username") String username) {        System.out.print(username);        return "index";    }}

@RequestMapping中定義訪問(wèn)頁(yè)面的URL模版,使用{}傳入頁(yè)面參數(shù),使用@PathVariable 獲取傳入?yún)?shù),即可通過(guò)地址:http://localhost:8080/crm/index/tanqimin.htm 訪問(wèn);

根據(jù)不同的Web請(qǐng)求方法,映射到不同的處理方法:

使用登陸頁(yè)面作示例,定義兩個(gè)方法分辨對(duì)使用GET請(qǐng)求和使用POST請(qǐng)求訪問(wèn)login.htm時(shí)的響應(yīng)。可以使用處理GET請(qǐng)求的方法顯示視圖,使用POST請(qǐng)求的方法處理業(yè)務(wù)邏輯;

@Controllerpublic class LoginController {    @RequestMapping(value = "/login", method = RequestMethod.GET)    public String login() {        return "login";    }    @RequestMapping(value = "/login", method = RequestMethod.POST)    public String login2(HttpServletRequest request) {            String username = request.getParameter("username").trim();            System.out.println(username);        return "login2";    }}

在視圖頁(yè)面,通過(guò)地址欄訪問(wèn)login.htm,是通過(guò)GET請(qǐng)求訪問(wèn)頁(yè)面,因此,返回登陸表單視圖login.jsp;當(dāng)在登陸表單中使用POST請(qǐng)求提交數(shù)據(jù)時(shí),則訪問(wèn)login2方法,處理登陸業(yè)務(wù)邏輯;

防止重復(fù)提交數(shù)據(jù),可以使用重定向視圖:

return "redirect:/login2"

可以傳入方法的參數(shù)類型:

@RequestMapping(value = "login", method = RequestMethod.POST)public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {	String username = request.getParameter("username");	System.out.println(username);	return null;}

 

可以傳入HttpServletRequest、HttpServletResponseHttpSession,值得注意的是,如果第一次訪問(wèn)頁(yè)面,HttpSession沒(méi)被創(chuàng)建,可能會(huì)出錯(cuò);

其中,String username = request.getParameter("username");可以轉(zhuǎn)換為傳入的參數(shù):

 

@RequestMapping(value = "login", method = RequestMethod.POST)public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {	String username = request.getParameter("username");	System.out.println(username);	return null;}

 

使用@RequestParam 注解獲取GET請(qǐng)求或POST請(qǐng)求提交的參數(shù);

獲取Cookie的值:使用@CookieValue 

獲取PrintWriter

可以直接在Controller的方法中傳入PrintWriter對(duì)象,就可以在方法中使用:

@RequestMapping(value = "login", method = RequestMethod.POST)public String testParam(PrintWriter out, @RequestParam("username") String username) {	out.println(username);	return null;}

 

 

獲取表單中提交的值,并封裝到POJO中,傳入Controller的方法里:

POJO如下(User.java):

public class User{	private long id;	private String username;	private String password;	…此處省略getter,setter...}

 

 

通過(guò)表單提交,直接可以把表單值封裝到User對(duì)象中:

@RequestMapping(value = "login", method = RequestMethod.POST)public String testParam(PrintWriter out, User user) {	out.println(user.getUsername());	return null;}

 

 

可以把對(duì)象,put 入獲取的Map對(duì)象中,傳到對(duì)應(yīng)的視圖:

@RequestMapping(value = "login", method = RequestMethod.POST)public String testParam(User user, Map model) {	model.put("user",user);	return "view";}

 

在返回的view.jsp中,就可以根據(jù)key來(lái)獲取user的值(通過(guò)EL表達(dá)式,${user }即可);

Controller中方法的返回值:

void:多數(shù)用于使用PrintWriter輸出響應(yīng)數(shù)據(jù);

String 類型:返回該String對(duì)應(yīng)的View Name;

任意類型對(duì)象:

返回ModelAndView

自定義視圖(JstlView,ExcelView):

攔截器(Inteceptors):

public class MyInteceptor implements HandlerInterceptor {	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) 		throws Exception {		return false;	}	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav) 		throws Exception {	}	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn) 		throws Exception {	}}

 

攔截器需要實(shí)現(xiàn)HandleInterceptor接口,并實(shí)現(xiàn)其三個(gè)方法:

preHandle:攔截器的前端,執(zhí)行控制器之前所要處理的方法,通常用于權(quán)限控制、日志,其中,Object o表示下一個(gè)攔截器;

postHandle:控制器的方法已經(jīng)執(zhí)行完畢,轉(zhuǎn)換成視圖之前的處理;

afterCompletion:視圖已處理完后執(zhí)行的方法,通常用于釋放資源;

MVC的配置文件中,配置攔截器與需要攔截的URL

<mvc:interceptors>	<mvc:interceptor>		<mvc:mapping path="/index.htm" />		<bean class="com.minx.crm.web.interceptor.MyInterceptor" />	</mvc:interceptor></mvc:interceptors>

 

國(guó)際化:

MVC配置文件中,配置國(guó)際化屬性文件:

<bean id="messageSource"	class="org.springframework.context.support.ResourceBundleMessageSource"	p:basename="message"></bean>

 

那么,Spring就會(huì)在項(xiàng)目中搜索相關(guān)的國(guó)際化屬性文件,如:message.properties、message_zh_CN.properties

VIEW中,引入Spring標(biāo)簽:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />調(diào)用,即可;

如果一種語(yǔ)言,有多個(gè)語(yǔ)言文件,可以更改MVC配置文件為:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">	<property name="basenames">		<list>			<value>message01</value>			<value>message02</value>			<value>message03</value>		</list>	</property></bean>

 

 

 

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SpingMVC ModelAndView, Model,Control以及參數(shù)傳遞
Springmvc
二、spring mvc模擬用戶增刪改查以及登錄和上傳文件的相關(guān)流程
spring框架下配置lucene
4 數(shù)據(jù)處理及跳轉(zhuǎn)
史上最全最強(qiáng)SpringMVC詳細(xì)示例實(shí)戰(zhàn)教程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服