springMVC_04controller四種配置總結
阿新 • • 發佈:2018-12-12
一.通過url對應bean,加粗部分為必須有的
<bean class=" org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置handlerAdapter ,這個配置可以沒有,但是配置請求和處理器時,name名稱必須要規範 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 配置渲染器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 將檢視名 渲染後檢視的字首 --> <propertyname="prefix" value="/WEB-INF/jsp/"/> <!-- 渲染後檢視的字尾 --> <property name="suffix" value=".jsp"/> <!-- 例:檢視名為:hello 渲染後:/WEB-INF/jsp/hello.jsp 該頁面--> </bean> <!-- 配置請求和處理器 --> <bean name="/hello.do" class="com.ahd.controller.HelloController"></bean>
二.為url分配bean
<?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" 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"> <!-- 配置渲染器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 將檢視名 渲染後檢視的字首 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 渲染後檢視的字尾 --> <property name="suffix" value=".jsp"/> <!-- 例:檢視名為:hello 渲染後:/WEB-INF/jsp/hello.jsp 該頁面--> </bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!-- key值為url,value為對應處理器的id --> <prop key="/hello.do">HelloController</prop> </props> </property> </bean> <bean id="HelloController" class="com.ahd.controller.HelloController"></bean>
三.為url匹配bean
<?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" 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"> <!-- 配置渲染器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 將檢視名 渲染後檢視的字首 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 渲染後檢視的字尾 --> <property name="suffix" value=".jsp"/> <!-- 例:檢視名為:hello 渲染後:/WEB-INF/jsp/hello.jsp 該頁面--> </bean> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <!-- 請求為hello*.do,都將被處理 稱之為匹配 --> <bean id="helloController" class="com.ahd.controller.HelloController"></bean> </beans>
四.使用註解
配置檔案
<?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" 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"> <!-- 配置渲染器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 將檢視名 渲染後檢視的字首 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 渲染後檢視的字尾 --> <property name="suffix" value=".jsp"/> <!-- 例:檢視名為:hello 渲染後:/WEB-INF/jsp/hello.jsp 該頁面--> </bean> <!—配置掃描指定包的註解--!> <context:component-scan base-package="com.ahd.controller"></context:component-scan> </beans>
控制器類HelloController
package com.ahd.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("hello") public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp){ ModelAndView mv=new ModelAndView(); mv.addObject("msg", "hello,welcome to use annotation"); mv.setViewName("hello"); return mv; } }