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>
相关推荐
主要介绍了SpringMVC基于注解的Controller详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springmvc非注解controller控制器
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。
springmvc和mybatis框架整合,Controller使用注解和非注解方式实现。
springmvc常用注解标签详解,@Controller控制器Controller 负责处理由DispatcherServlet 分发的请求,@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有...
springmvc注解简单实例,本例子从dao层service层controller层,层层采用注解形式,层层调用,紧密相连,通过这个简单的例子我相信大家会对springmvc又一个整体大概的了解
该文档是关于SpringMVC注解开发的详解,如:@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@...
创建基于注解控制器的应用实现(注册和登录) 1.目录结构 3.3 Controller 文件 indexController package com.dhl.backstage.controller; import org.springframework.stereotype.Controller; import org....
注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @...
@RequestMapping 注解中除了 params 属性外,还有一个常用的属性是 method,它可以让 Controller 方法处理特定 HTTP 请求方式的请求,如让一个方法处理 HTTP GET 请求,而另一个方法处理 HTTP POST 请求,如下所示:...
之前也发了一个,这个包含之前的,还添加了hibernate对数据库操作的代码(注意:controller层两个QBC/SQLTest代码不能运行,只是写出了操作数据库的除了HQL的另外2种方式)。我发现现在还是有很多公司用hibernate+...
本文内容包括:概述一个简单的基于注解的Controller清单3.annomvc-servlet.xml让一个Controller处理多个URL请求清单3.每个请求处理参数对应一个URL处理方法入参如何绑定URL参数清单5.按参数名匹配进行绑定清单8.通过...
详细介绍了springmvc的注解 如@Controller,@RequestMapping等,详细到你无法想象,采用例子代码的方式,一目了然。
此demo是基于注解的SpringMVC,结构清晰,新手可以参考一下,所有用到的jar包我都打包进去了;
SpringMVC+Mybatis整合及dao、service强封装:一、SPringMVC与Mybatis框架整合,其中SpringMVC主要采用注解方式实现,Mybatis采用xml为主,对数据库开发、测试、部署环境进行分离配置;二、在封装上针对Mybatis进行...
Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2
学习Spring MVC,关于注解的Spring MVC,简单例子,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例
本篇文章主要介绍了SpringMVC Controller介绍及常用注解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了聊聊springmvc中controller的方法的参数注解方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下