05-springmvc之ssm二(案例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linzhaoliangyan/article/details/88804612

* Json数据交互(@RequestBody,@ResponseBody)    

1 @RequestBody 作用:
@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到controller方法的参数上。
2 @ResponseBody 作用:
该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端

3 环境准备
 * Springmvc默认用MappingJackson2HttpMessageConverter对json数据进行转换,需要加入jackson的包
  <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.7</version>
   </dependency>
   <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.7</version>
</dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.7</version>
    </dependency>
注意:
Spring3.x 用MappingJacksonHttpMessageConverter
Spring4.x 用MappingJackson2HttpMessageConverter
配置json转换器(了解)
在注解适配器中加入messageConverters
<!--注解适配器 -->
    <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <propertyname="messageConverters">
        <list>
        <beanclass="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
        </list>
        </property>
    </bean>

注意:如果使用<mvc:annotation-driven /> 则不用定义上边的内容。

原理: 通过 @requestBody  @responseBody 
  框架会自动去找JSON转换器(jaksonjar包中)
配置好了<mvc:annotation-driven />之后,框架自动加载转换器。

java.lang.IllegalArgumentException: No converter found for return value of type: class com.hx.ssm.entity.vo.ZnodeDataVo

 "application/json" using [org.springframework.http.converter.json.GsonHttpMessageConverter@7dbe2a3e]

* 准备之前报表的环境

 * 控制器的编写

@Controller
public class StatisticsController {
    @Autowired
    private StatisticsSexService mStatisticsSexService;

    @RequestMapping(value = "/getClassesData", method = RequestMethod.GET)
    @ResponseBody
    public BaseDataVo getClassesData() {
        return mStatisticsSexService.getClassesData();
    }

    @RequestMapping(value = "/statisticsSex", method = RequestMethod.GET)
    @ResponseBody
    public BaseDataVo statisticsSex(HttpServletRequest request) {
        int cno = Integer.parseInt(request.getParameter("cno"));
        return mStatisticsSexService.statisticsSexByCno(cno);
    }

}


@Controller
public class ZNodeController {
    @Autowired
    private ZNodeService mZNodeService;

    @RequestMapping("/queryAllZnode")
    @ResponseBody
    public BaseDataVo queryAllZNode() {
        return mZNodeService.getAllZNodes();
    }
}

  * 页面访问路径后面加.do   

${pageContext.request.contextPath}/statisticsSex.do?cno="+selValue
${pageContext.request.contextPath}/getClassesData.do
${pageContext.request.contextPath}/queryAllZnode.do   

* 获得父子容器里面bean的内容

@RequestMapping("/hello2")
    public void hello2(HttpServletRequest request, HttpServletResponse response) {
        // 父容器
        WebApplicationContext parentContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        int beanCount = parentContext.getBeanDefinitionCount();
        System.out.println("parent-beanCount:" + beanCount);
        String[] beanDefinitionNames = parentContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames) {
            System.out.println("parent:" + beanName);
        }

        //子容器
        WebApplicationContext childContext = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        beanCount = childContext.getBeanDefinitionCount();
        System.out.println("child-beanCount:" + beanCount);
        beanDefinitionNames = childContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames) {
            System.out.println("child:" + beanName);
        }
    }

 * use-default-filters 设置为true

parent-beanCount:22
parent:configProperties
parent:org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer#0
parent:c3p0
parent:ssf
parent:org.mybatis.spring.mapper.MapperScannerConfigurer#0
parent:transactionManager
parent:org.springframework.transaction.support.DefaultTransactionDefinition#0
parent:dataVoAndTransactionAspect
parent:classService
parent:statisticsSexService
parent:transferService
parent:nodeService
parent:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
parent:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
parent:org.springframework.context.annotation.internalRequiredAnnotationProcessor
parent:org.springframework.context.annotation.internalCommonAnnotationProcessor
parent:org.springframework.context.event.internalEventListenerProcessor
parent:org.springframework.context.event.internalEventListenerFactory
parent:org.springframework.aop.config.internalAutoProxyCreator
parent:classesMapper
parent:studentMapper
parent:ZNodeMapper
child-beanCount:29
child:dataVoAndTransactionAspect
child:helloController
child:statisticsController
child:ZNodeController
child:classService
child:statisticsSexService
child:transferService
child:nodeService
child:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
child:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
child:org.springframework.context.annotation.internalRequiredAnnotationProcessor
child:org.springframework.context.annotation.internalCommonAnnotationProcessor
child:org.springframework.context.event.internalEventListenerProcessor
child:org.springframework.context.event.internalEventListenerFactory
child:mvcContentNegotiationManager
child:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
child:mvcCorsConfigurations
child:org.springframework.format.support.FormattingConversionServiceFactoryBean#0
child:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
child:mvcUriComponentsContributor
child:org.springframework.web.servlet.handler.MappedInterceptor#0
child:org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
child:org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0
child:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0
child:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
child:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
child:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
child:mvcHandlerMappingIntrospector
child:org.springframework.web.servlet.view.

 

* use-default-filters 设置为false

parent-beanCount:22
parent:configProperties
parent:org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer#0
parent:c3p0
parent:ssf
parent:org.mybatis.spring.mapper.MapperScannerConfigurer#0
parent:transactionManager
parent:org.springframework.transaction.support.DefaultTransactionDefinition#0
parent:dataVoAndTransactionAspect
parent:classService
parent:statisticsSexService
parent:transferService
parent:nodeService
parent:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
parent:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
parent:org.springframework.context.annotation.internalRequiredAnnotationProcessor
parent:org.springframework.context.annotation.internalCommonAnnotationProcessor
parent:org.springframework.context.event.internalEventListenerProcessor
parent:org.springframework.context.event.internalEventListenerFactory
parent:org.springframework.aop.config.internalAutoProxyCreator
parent:classesMapper
parent:studentMapper
parent:ZNodeMapper
child-beanCount:24
child:helloController
child:statisticsController
child:ZNodeController
child:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
child:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
child:org.springframework.context.annotation.internalRequiredAnnotationProcessor
child:org.springframework.context.annotation.internalCommonAnnotationProcessor
child:org.springframework.context.event.internalEventListenerProcessor
child:org.springframework.context.event.internalEventListenerFactory
child:mvcContentNegotiationManager
child:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
child:mvcCorsConfigurations
child:org.springframework.format.support.FormattingConversionServiceFactoryBean#0
child:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
child:mvcUriComponentsContributor
child:org.springframework.web.servlet.handler.MappedInterceptor#0
child:org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
child:org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0
child:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0
child:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
child:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
child:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
child:mvcHandlerMappingIntrospector
child:org.springframework.web.servlet.view.InternalResourceViewResolver#0

use-default-filters:
* SpringMVC容器不仅仅扫描并注册带有@Controller注解的Bean,而且还扫描并注册了带有@Component的子注解@Service、@Reposity的Bean。因为use-default-filters默认为true。所以如果不需要默认的,则use-default-filters=“false”禁用掉。
* 当我们进行上面的配置时,SpringMVC容器会把service的bean重新加载,从而造成新加载的bean覆盖了老的bean,但事务的AOP代理没有配置在spring-mvc.xml配置文件中,造成事务失效。解决办法是:在spring-mvc.xml配置文件中的context:component-scan标签中使用use-default-filters=“false”禁用掉默认的行为

* 把jsp文件放进WEB-INF/jsps/ 目录

StatisticsController
@RequestMapping("/statistics")
    public ModelAndView statistics(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("statistics");
        return modelAndView;
    }
ZNodeController
 @RequestMapping("/ztree")
    public ModelAndView ztree(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("ztree");
        return modelAndView;
    }

  *ztree.jsp

* Controller 的细节

 @RequestMapping("/hello")
  public ModelAndView hello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", "xiaohei");
        modelAndView.setViewName("hello");
        return modelAndView;
    }
简化写法
  @RequestMapping("/hello3")
 public String hello3(Model model) {
   model.addAttribute("name","xiaohei123");
    return "hello";
  }

猜你喜欢

转载自blog.csdn.net/linzhaoliangyan/article/details/88804612