springmvc使用和经验总结(2)

简介: springmvc使用和经验总结

然后在application.xml里面配置

<?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:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd     
           http://www.springframework.org/schema/tx   
           http://www.springframework.org/schema/tx/spring-tx.xsd    
           http://www.springframework.org/schema/aop     
           http://www.springframework.org/schema/aop/spring-aop.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc.xsd   
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd">
  <!-- 自动扫描的包名 -->
  <context:component-scan base-package="com.shishuo.studio"></context:component-scan>
  <mvc:annotation-driven />
  <task:annotation-driven />
  <tx:annotation-driven />
  <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  <bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/**" />
      <bean class="com.shishuo.studio.filter.GlobalInterceptor"></bean>
    </mvc:interceptor>
    <mvc:interceptor>
      <mvc:mapping path="/auth/**" />
      <bean class="com.shishuo.studio.filter.AuthInterceptor"></bean>
    </mvc:interceptor>
    <mvc:interceptor>
      <mvc:mapping path="/auth/studio/**" />
      <bean class="com.shishuo.studio.filter.StudioInterceptor"></bean>
    </mvc:interceptor>
  </mvc:interceptors>
  <!-- 在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码 -->
  <bean id="propertyConfigurer" class="com.shishuo.studio.util.PropertyUtils">
    <property name="locations">
      <list>
        <value>classpath:shishuo.studio.properties</value> <!-- 指定外部文件的编码 -->
      </list>
    </property>
  </bean>
  <!-- FreeMarker的配置 -->
  <bean id="freeMarkerConfigurer"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftl" /><!-- 
      指定路径 -->
    <property name="defaultEncoding" value="UTF-8" /><!-- 指定编码格式 -->
    <property name="freemarkerSettings">
      <props>
        <prop key="template_update_delay">10</prop>
        <prop key="defaultEncoding">UTF-8</prop>
        <prop key="url_escaping_charset">UTF-8</prop>
        <prop key="locale">zh_CN</prop>
        <prop key="boolean_format">true,false</prop>
        <prop key="time_format">HH:mm:ss</prop>
        <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
        <prop key="date_format">yyyy-MM-dd</prop>
        <prop key="number_format">#.##</prop>
        <prop key="whitespace_stripping">true</prop>
        <prop key="classic_compatible">true</prop>
      </props>
    </property>
  </bean>
  <!-- 配置 FreeMarker视图解析器 -->
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass"
      value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
    <property name="cache" value="false" />
    <property name="prefix" value="/" />
    <property name="suffix" value=".ftl" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
    <property name="contentType" value="text/html;charset=utf-8" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="exposeSpringMacroHelpers" value="true" />
  </bean>
</beans>

@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。

@Component  Component是Spring管理组件的通用形式,而@repository,@Service,@Controller是它的细化。分别表示更加具体的用例(分别对应持久化层,服务层和表现层)




B、按照Class路径扫描

XML风格的配置方式,我们会在配置文件中配置大量的bean,这样但项目足够大时,那么这个配置文件将过于庞大而不便管理。而应用@注释的配置方式,我们在类中用@Component等注释类,并让容器按照Classpath自动扫描管理它们。要实现以上功能我们需要这样定义。

<?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:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

</beans>

在使用组件扫描时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor也将隐式包含进来,也就是说它支持@Autowired等,不需要我们如用<context:annotation-config/>再做声明了。。

自动扫描包名的配置 <context:component-scan base-package="com.shishuo"></context>



当我们用spring mvc 前端控制器的时候需要配置



<!-- spring mvc 基于注解在方法上 控制映射 配置 -->

<bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean

class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

</bean>



<!-- 在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:shishuocms.properties</value> <!-- 指定外部文件的编码 -->

</list>

</property>

</bean>



<!-- 配置 FreeMarker视图解析器 -->

<bean id="viewResolver"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<property name="viewClass"

value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>

<property name="cache" value="false" />

<property name="prefix" value="/" />

<property name="suffix" value=".ftl" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->

<property name="contentType" value="text/html;charset=utf-8" />

<property name="exposeRequestAttributes" value="true" />

<property name="exposeSessionAttributes" value="true" />

<property name="exposeSpringMacroHelpers" value="true" />

</bean>



<!--创建数据映射器,数据映射器必须为接口 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="annotationClass" value="org.springframework.stereotype.Repository" />

<property name="basePackage" value="com.shishuo.cms.dao" />

</bean>

spring mvc 拦截器



拦截器在application.xml配置



<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/**" />

<bean class="com.shishuo.cms.filter.GlobalInterceptor"></bean>

</mvc:interceptor>

</mvc:interceptors>



怎么使用RequestMapping的参数 主要有RequestParam Pathvariable(这个注解是获取url里面的参数)



5.1.1、常见应用场景

1、日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。

2、权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;

3、性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);

4、通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实现。

5、OpenSessionInView:如Hibernate,在进入处理器打开Session,在完成后关闭Session。

…………本质也是AOP(面向切面编程),也就是说符合横切关注点的所有功能都可以放入拦截器实现。



MySQL server version for the right syntax to use near  where userId=28  at line 1

这个错误是在写修改语句的时候 where 前面多加了一个逗号

syntax error 是语法错误

Could not resolve view with name 'auth/teacher/skill/update' in servlet with是自己没有加@Responsebody 用spring mvc 的时候返回是json 一定要记得写@Responsebody



当需要上传一个form里面包含 文件 或者视屏的时候 一定要记得在form表单后面添加 enctype="multipart/form-data" enctype="multipart/form-data"



@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

分类: spring 2012-09-21 16:22 11494人阅读 评论(4) 收藏 举报

目录(?)[+]

引言:

接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用;





简介:

handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)



A、处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解:   @PathVariable;

B、处理request header部分的注解:   @RequestHeader, @CookieValue;

C、处理request body部分的注解:@RequestParam,  @RequestBody;

D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;


相关文章
|
19天前
|
SQL Java 数据库连接
对Spring、SpringMVC、MyBatis框架的介绍与解释
Spring 框架提供了全面的基础设施支持,Spring MVC 专注于 Web 层的开发,而 MyBatis 则是一个高效的持久层框架。这三个框架结合使用,可以显著提升 Java 企业级应用的开发效率和质量。通过理解它们的核心特性和使用方法,开发者可以更好地构建和维护复杂的应用程序。
109 29
|
2月前
|
设计模式 前端开发 Java
步步深入SpringMvc DispatcherServlet源码掌握springmvc全流程原理
通过对 `DispatcherServlet`源码的深入剖析,我们了解了SpringMVC请求处理的全流程。`DispatcherServlet`作为前端控制器,负责请求的接收和分发,处理器映射和适配负责将请求分派到具体的处理器方法,视图解析器负责生成和渲染视图。理解这些核心组件及其交互原理,有助于开发者更好地使用和扩展SpringMVC框架。
67 4
|
3月前
|
前端开发 Java 开发者
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
234 2
|
4月前
|
JSON 前端开发 Java
SSM:SpringMVC
本文介绍了SpringMVC的依赖配置、请求参数处理、注解开发、JSON处理、拦截器、文件上传下载以及相关注意事项。首先,需要在`pom.xml`中添加必要的依赖,包括Servlet、JSTL、Spring Web MVC等。接着,在`web.xml`中配置DispatcherServlet,并设置Spring MVC的相关配置,如组件扫描、默认Servlet处理器等。然后,通过`@RequestMapping`等注解处理请求参数,使用`@ResponseBody`返回JSON数据。此外,还介绍了如何创建和配置拦截器、文件上传下载的功能,并强调了JSP文件的放置位置,避免404错误。
|
4月前
|
前端开发 Java 应用服务中间件
【Spring】Spring MVC的项目准备和连接建立
【Spring】Spring MVC的项目准备和连接建立
79 2
|
4月前
|
XML 前端开发 Java
Spring,SpringBoot和SpringMVC的关系以及区别 —— 超准确,可当面试题!!!也可供零基础学习
本文阐述了Spring、Spring Boot和Spring MVC的关系与区别,指出Spring是一个轻量级、一站式、模块化的应用程序开发框架,Spring MVC是Spring的一个子框架,专注于Web应用和网络接口开发,而Spring Boot则是对Spring的封装,用于简化Spring应用的开发。
323 0
Spring,SpringBoot和SpringMVC的关系以及区别 —— 超准确,可当面试题!!!也可供零基础学习
|
5月前
|
XML 缓存 前端开发
springMVC02,restful风格,请求转发和重定向
文章介绍了RESTful风格的基本概念和特点,并展示了如何使用SpringMVC实现RESTful风格的请求处理。同时,文章还讨论了SpringMVC中的请求转发和重定向的实现方式,并通过具体代码示例进行了说明。
springMVC02,restful风格,请求转发和重定向
|
5月前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
|
6月前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
6月前
|
前端开发 应用服务中间件 数据库
SpringMVC入门到实战------八、RESTful案例。SpringMVC+thymeleaf+BootStrap+RestFul实现员工信息的增删改查
这篇文章通过一个具体的项目案例,详细讲解了如何使用SpringMVC、Thymeleaf、Bootstrap以及RESTful风格接口来实现员工信息的增删改查功能。文章提供了项目结构、配置文件、控制器、数据访问对象、实体类和前端页面的完整源码,并展示了实现效果的截图。项目的目的是锻炼使用RESTful风格的接口开发,虽然数据是假数据并未连接数据库,但提供了一个很好的实践机会。文章最后强调了这一章节主要是为了练习RESTful,其他方面暂不考虑。
SpringMVC入门到实战------八、RESTful案例。SpringMVC+thymeleaf+BootStrap+RestFul实现员工信息的增删改查