環(huán)境的搭建
使用工具:MyEclipse, tomcat7,SpringMVC3.2
新建JAVA web Project纸颜,在WEB-INF/lib下導(dǎo)入相對(duì)應(yīng)的jar包如下圖:
- 配置WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>FirstWeb</display-name>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
<!--springmvc 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器,適配器等等) -->
<!-- 如果不配置這個(gè)東西涮较,會(huì)默認(rèn)加載/web-inf/servlet名稱-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
<!--
第一種:*.action:訪問以.action結(jié)尾由DispatcherServlet進(jìn)行解析
第二種:/:所有的訪問都由DispatcherServlet進(jìn)行解析 候齿,對(duì)于靜態(tài)的文件的解析需要配置不讓DispatcherServlet進(jìn)行解析闺属,
使用這種方式的可以實(shí)現(xiàn)RESTful風(fēng)格的url
第三種:/*:這樣配置不對(duì)掂器,使用這種配置国瓮,最終都要轉(zhuǎn)發(fā)到一個(gè)jsp頁(yè)面匠楚,仍然會(huì)由DispatcherServlet進(jìn)行解析芋簿,但是不能根據(jù)jsp頁(yè)面找到headler璃饱,會(huì)報(bào)錯(cuò)荚恶。
-->
</servlet-mapping>
</web-app>
- 其中我們指定了我們的前端控制器為:
<param-value>classpath:springmvc.xml</param-value>
-
所以我們需要新建資源目錄谒撼,與src平級(jí):
- 在里面新建立:springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 配置Handler -->
<bean name="/queryItems.action"
class="com.ly.springmvc.controller.ItemsController1"></bean>
<!-- 處理器映射器
將bean的name做為url來進(jìn)行查找,需要在配置Handler時(shí)指定beanname
(也就是url)
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 處理器適配器
所有的適配器都要實(shí)現(xiàn)HandlerAdapter
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 視圖解析器
解析jsp視圖呻畸,默認(rèn)使用jstl標(biāo)簽伤为,前提是得保證classpath下得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
- 基本搭建完成绞愚,那我們可以先寫一個(gè)簡(jiǎn)單的Controller:
/**
* 實(shí)現(xiàn)Controller的接口的處理器
* @author Ly
*
*/
public class ItemsController1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
//調(diào)用Service查找數(shù)據(jù)庫(kù)位衩,查詢商品列表
List<Items> list=new ArrayList<Items>();
list.add(new Items("Ly",11111,"這是凌宇"));
list.add(new Items("Ly1",22222,"這是凌宇1"));
list.add(new Items("Ly2",333333,"這是凌宇2"));
// 返回ModelAndView
ModelAndView modelAndView =new ModelAndView();
// 相當(dāng)于request的setAttribut,在jsp頁(yè)面中通過itemsList獲取數(shù)
modelAndView.addObject("itemsList",list);
// 指定視圖
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
- 由于我們指定了一個(gè)jsp目錄哪痰,所以我們?cè)谀夸浵滦陆⒁粋€(gè)jsp:
-
運(yùn)行后得到結(jié)果:
使用注解模式
在上面的配置都是非注解模式晌杰,那么我們可以使用注解模式來進(jìn)行開發(fā):
<!-- 以上為非注解的東西筷弦,下面為注解的配置 -->
<!-- 注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 注解適配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>