`

SpringMVC (二)基于注解方式的Controller

阅读更多

SpringMVC在实际的开发应用过程中,通常采用注解的方式。

第一步,告知Spring,Controller放在哪个路径下,让Spring容器自动去寻找和装配Controller。

<context:component-scan base-package="org.lian.controller"/>

 第二步,SpringMVC中HandlerMapping和HandlerAdapter是成对出现,接下来就是配置这两个对象。

<!-- 采用注解的方式    HandlerMapping 和  HandlerAdapter-->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
	        <util:list id="beanList">
	        	<ref bean="stringHttpMessageConverter"/>
	            <ref bean="gsonHttpMessageConverter"/>
	        </util:list>
	    </property>
	</bean>
	<bean id="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
       </property>
        
    </bean>
        
	<!-- 通过Gson来进行Json数据格式转化 配置转化器 -->
	<bean id="gsonHttpMessageConverter"
        class="org.springframework.http.converter.json.GsonHttpMessageConverter">
        <property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
       </property>
    </bean>

 对于不同格式数据要配置不同的Converter数据转化器,上面的配置中要处理JSON格式的数据,配置了JSON转化器,使用Gson来处理json数据,配置相应的转化器类。

第三步 路径映射和返回数据

@RequestMapping("city")
@Controller
public class CityController {

	private CityService cityService;

	public CityService getCityService() {
		return cityService;
	}

	@Autowired
	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	/**
	 * 
	 * @description TODO
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping(path = "/view/list", method = RequestMethod.GET)
	public ModelAndView listProvincePage() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("division/cityList");
		return mv;
	}

	@RequestMapping(path = "/handler/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String listCity(@RequestBody Map<String, String> request) {

		/**
		 * 方法中参数 如果使用 Map<String,Object>类型, 那么SpringMVC绑定参数在这里会将数值解析为float类型
		 * 
		 * 如limit 10 会解析成 10.0
		 * 
		 */
		int limit = Integer.parseInt(request.get(BootstrapTableContants.LIMIT));
		int offset = Integer.parseInt(request.get(BootstrapTableContants.OFFSET));
		return cityService.pageCity(null, offset, limit);
	}

	@RequestMapping(path = "/handler/listProvince", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String listProvince() {
		return cityService.listProvince();
	}

	@RequestMapping(path = "/handler/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String addCity(@RequestBody City city) {
		return cityService.addCity(city);
	}

	@RequestMapping(path = "/handler/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String editCity(@RequestBody City city) {
		return cityService.editCity(city);
	}

	@RequestMapping(path = "/handler/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String deleteCity(@RequestBody City city) {
		return cityService.deleteCityById(city.getCityId());
	}

	
}

 在返回ModelAndView是返回视图层更具体的是指JSP页面,如果要返回JSON格式数据,可以通过Gson在程序里将手动转化,也可直接将对象返回,因为在前面已经配置了转化器,SpringMVC会通过合适自动给转化。需要特别指出在返回JSON数据时要使用@ResponseBody。

    SpringMVC允许访问静态资源,脚本图片CSS等,需要添加下面的配置

<!-- 开启静态资源缓存  -->
	<mvc:resources mapping="/resources/**" location="/resources/">
    	<mvc:cache-control max-age="3600" cache-public="true"/>
	</mvc:resources>
	
	<!-- <mvc:resources mapping="/resources/**" location="/resources/">
	</mvc:resources> -->

 最后完整的SpringMVC配置如下,特别指出需要处理特殊数据,需要配置其他的转化器,这里没有全部罗列,并且也没有提到SpringMVC上传文件需要的转化器等。

<?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"
    xmlns:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="org.lian.controller"/>
	
	<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"/>
	</bean>
	<!-- 根据 Bean的name来映射路径-->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
	
	<!-- <bean class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter"/> -->
	<!-- 根路径/ 不可省略 否则找不到对象 -->
	<bean name="/helloWorld" class="org.lian.controller.HelloWorldController"/>
	<!-- 采用注解的方式    HandlerMapping 和  HandlerAdapter-->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
	        <util:list id="beanList">
	        	<ref bean="stringHttpMessageConverter"/>
	            <ref bean="gsonHttpMessageConverter"/>
	        </util:list>
	    </property>
	</bean>
	<bean id="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
       </property>
        
    </bean>
        
	<!-- 通过Gson来进行Json数据格式转化 配置转化器 -->
	<bean id="gsonHttpMessageConverter"
        class="org.springframework.http.converter.json.GsonHttpMessageConverter">
        <property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
       </property>
    </bean>
        
	<!-- 开启静态资源缓存  -->
	<mvc:resources mapping="/resources/**" location="/resources/">
    	<mvc:cache-control max-age="3600" cache-public="true"/>
	</mvc:resources>
	
	<!-- <mvc:resources mapping="/resources/**" location="/resources/">
	</mvc:resources> -->
</beans>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics