-
配置pom文件依賴庫版本房交,及spring依賴管理 (Maven自動來管理Spring版本)
<properties> <spring.version>4.1.3.RELEASE</spring.version> <commons.lang.version>2.6</commons.lang.version> <slf4j.version>1.7.6</slf4j.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
bom : 材料清單
Spring為了解決jar包沖突产徊,推出spring-framework-bom持际、spring-boot-dependencies焊唬、platform-bom用來統(tǒng)一管理Spring版本蒋纬。
-
導(dǎo)入 Spring MVC 及其依賴jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.lang.version}</version> </dependency>
-
配置web.xml 加入Spring核心類 DispacherServlet
<?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"> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet 對應(yīng)的上下文配置,默認為WEB-INF/${servlet-name}-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/mvc-dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <!--默認攔截所有請求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
init-param
可以不去配置源梭,省略后默為/WEB-INF/${servlet-name}-servlet.xml
配置時配置contextConfigLocation參數(shù) value = url
-
web-app
2.3及之前版本默認關(guān)閉el表達式
2.4版本默認開啟el表達式
-
-
添加Spring MVC 配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--激活注解配置--> <context:annotation-config/> <!--配置只關(guān)注@Contraller標注的Bean--> <context:component-scan base-package="com.bjchaney.springmvc"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--HandlerMapping 不需要配置 SpringMVC 默認啟用DefaultAnnotationHandlerMapping--> <!--擴充了注解驅(qū)動--> <mvc:annotation-driven/> <!--引入靜態(tài)資源--> <mvc:resources mapping="/resources/*" location="/resources/"/> <!--配置ViewResolver 可以配置多個ViewResolver, 使用order屬性排序, InternalResourceViewResolver 放在最后 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置使用支持jstl的view--> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
- Spring-MVC 所有的配置完成
- 之后加入Spring來管理Service層和dao層铺呵,實現(xiàn)ApplicationContext層次化
-
在 web.xml中加入Spring應(yīng)用的上下文
<!--Spring 應(yīng)用上下文 層次化 applicationContext--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
-
添加Spring容器的配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--激活注解配置--> <context:annotation-config/> <!--配置關(guān)注除@Contraller標注之外的Bean--> <context:component-scan base-package="com.bjchaney.springmvc"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
-
添加常用 ViewResolver
<!--文件上傳viewResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2000000"/> <property name="defaultEncoding" value="UTF-8"/> <property name="resolveLazily" value="true"/> </bean> <!-- ContentNegotiatingViewResolver 提供相同內(nèi)容不同表現(xiàn)形式的viewResolver 此處用來返回Json數(shù)據(jù) --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> <entry key="html" value="text/html"/> </map> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> </bean> </list> </property> <property name="ignoreAcceptHeader" value="true"/> </bean>
-
編寫Controller進行 相應(yīng)操作Example
@Controller @RequestMapping("/hello") public class HelloController { @Autowired UserService userService; @RequestMapping(value = "/impl1",method = RequestMethod.GET) public String hello(String name,Model model){ model.addAttribute("name", userService.getFotmatName(name)); return "hello"; } @RequestMapping(value = "/impl2",method = RequestMethod.GET) public String hello(String name,Map<String,String> model){ model.put("name", userService.getFotmatName(name)); return "hello"; } @RequestMapping(value = "/impl3",method = RequestMethod.GET) public String hello(HttpServletRequest request){ String name = request.getParameter("name"); request.setAttribute("name", userService.getFotmatName(name)); return "hello"; } @RequestMapping(value = "/json",method = RequestMethod.GET) @ResponseBody public RestResponse<String> hello(@ModelAttribute("name") String name){ RestResponse restResponse = new RestResponse(); return restResponse.success(name); } @RequestMapping(value = "/upload",method = RequestMethod.GET) public String hello(){ return "upload"; } @RequestMapping(value = "/doUpload",method = RequestMethod.POST) public String doUpload(MultipartFile file) throws Exception{ if(!file.isEmpty()){ userService.CopyFile(file.getInputStream(), "~/Documents/1.jpg"); } return "success"; } }
Spring MVC Quickly Start
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來姆打,“玉大人良姆,你說我怎么就攤上這事♂O罚” “怎么了玛追?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長闲延。 經(jīng)常有香客問我痊剖,道長,這世上最難降的妖魔是什么慨代? 我笑而不...
- 正文 為了忘掉前任邢笙,我火速辦了婚禮,結(jié)果婚禮上侍匙,老公的妹妹穿的比我還像新娘氮惯。我一直安慰自己,他們只是感情好想暗,可當我...
- 文/花漫 我一把揭開白布妇汗。 她就那樣靜靜地躺著顶伞,像睡著了一般耗绿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乙漓,一...
- 文/蒼蘭香墨 我猛地睜開眼驮配,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了着茸?” 一聲冷哼從身側(cè)響起壮锻,我...
- 正文 年R本政府宣布,位于F島的核電站奏夫,受9級特大地震影響怕篷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜酗昼,卻給世界環(huán)境...
- 文/蒙蒙 一廊谓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧麻削,春花似錦蒸痹、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扫责,卻和暖如春榛鼎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鳖孤。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理拾碌,服務(wù)發(fā)現(xiàn)吐葱,斷路器,智...
- Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
- 一、配置maven的pom.xml加載jar包 為了后續(xù)開發(fā)的方便防症,將SSM框架所有需要的jar包一并加載進來 p...
- 一步一步的搭建JAVA WEB項目孟辑,采用Maven構(gòu)建哎甲,基于MYBatis+Spring+Spring MVC+B...