springmvc都是經過其中央控制器DispatcherServlet來進行轉發的,因此在web.xml文件裏面須要增長相應的配置 html
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <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>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
若是沒有配置contextConfigLocation,Spring mvc默認配置文件爲「/WEB-INF/[servlet名字]-servlet.xml「即「/WEB-INF/springServlet-servlet.xml「 java
1、簡單的controller配置 web
springServlet-servlet.xml配置文件以下 spring
<!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <bean name="/hello" class="dk.web.controller.HelloWorldController"></bean></beans>
HelloWorldController 瀏覽器
public class HelloWorldController implements Controller { public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { //一、收集參數、驗證參數 //二、綁定參數到命令對象 //三、將命令對象傳入業務對象進行業務處理 //四、選擇下一個頁面 ModelAndView mv = new ModelAndView(); //添加模型數據 能夠是任意的POJO對象 mv.addObject("message", "Hello World,spring mvc"); //設置邏輯視圖名,視圖解析器會根據該名字解析到具體的視圖頁面 mv.setViewName("hello"); return mv; } }
/WEB-INF/view/hello.jsp spring-mvc
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> ${message} </body> </html>
在web容器裏面部署及運行你的工程,在瀏覽器裏面訪問 ip:port/hello 便可在頁面看到 Hello World,spring mvc mvc
2、基於註解anotation的controller配置 app
Spring2.5 引入註解式處理器支持,經過@Controller 和@RequestMapping註解定義咱們的處理器類。而且提供了一組強大的註解:須要經過處理器映射DefaultAnnotationHandlerMapping和處理器適配器AnnotationMethodHandlerAdapter來開啓支持@Controller 和 @RequestMapping 註解的處理器。
@Controller:用於標識是處理器類;
@RequestMapping:請求處處理器功能方法的映射規則。 jsp
<beans> <!-- HandlerMapping --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
PS:spring3.2版本後 DefaultAnnotationHandlerMapping由RequestMappingHandlerMapping取代;AnnotationMethodHandlerAdapter由RequestMappingHandlerAdapter取代 ui
HelloWorldController
@Controller public class HelloWorldController { @RequestMapping(value="/helloAnotation",method=RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp){ ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello World,spring anotation"); mv.setViewName("hello"); return mv; } }
在web容器裏面部署及運行你的工程,在瀏覽器裏面訪問 ip:port/helloAnotation便可在頁面看到 Hello World,spring anotation