zoukankan      html  css  js  c++  java
  • SSM案例整合踩的一些坑

    一、出现错误:Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource'

    无法将你的datasource里配置的字符串转换成javax.sql.DataSource对象,导致SessionFactory无法完成,datasource配置肯定有误,检查[/WEB-INF/applicationContext.xml]文件中的datasource相关的配置。
    <property name="dataSource" value="dataSource">
    配置错了,这写法Spring会以字符串方式注入,报类型不匹配异常,改为
    <property name="dataSource" ref="dataSource"/>

    二、WEB-INF下的jsp文件一般只能通过请求然后后台controller层才能跳转,直接请求该路径下的文件是进不去的会报404,而如果直接请求文件路径地址,如<a href="${pageContext.request.contextPath }/Novel.jsp/>那么该文件是需要在Webapp文件夹下的。


    三、spring mvc绑定参数之类型转换有三种方式:

    1.实体类中加日期格式化注解

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    private Date creationTime;

    只是解决了局部问题,解决不了根本问题

    2.属性编辑器(一般不用了)

    spring3.1之前

    在Controller类中通过@InitBinder完成

    /**
    * 在controller层中加入一段数据绑定代码
    * @param webDataBinder
    */
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) throws Exception{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    simpleDateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
    }
    备注:自定义类型转换器必须实现PropertyEditor接口或者继承PropertyEditorSupport类
    写一个类 extends propertyEditorSupport(implements PropertyEditor){
    public void setAsText(String text){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm");
    Date date = simpleDateFormat.parse(text);
    this.setValue(date);
    }
    public String getAsTest(){
    Date date = (Date)this.getValue();
    return this.dateFormat.format(date);
    }
    }

    3. 类型转换器Converter(用的比较多,虽然繁琐,但是治本)

     1 public class DateConvert implements Converter<String, Date> {
     2 private String pattern;
     3 
     4 public void setPattern(String pattern) {
     5   this.pattern = pattern;
     6 }
     7 
     8 @Override
     9 public Date convert(String source) {
    10   SimpleDateFormat format = new SimpleDateFormat(pattern);
    11   Date d = null;
    12 
    13 try {
    14   d = format.parse(source);
    15   } catch (ParseException e) {
    16   e.printStackTrace();
    17   }
    18 
    19   return d;
    20   }
    21 }

    在springMVC.xml中的配置

     1 <mvc:annotation-driven conversion-service="conversionService"/>
     2 <!-- 自定义的日期转换器-->
     3 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
     4 <property name="converters">
     5 <set>
     6 <bean class="edu.kust.utils.DateConvert">
     7 <!-- 自定义日期格式-->
     8 <property name="pattern" value="yyyy-MM-dd"/>
     9 </bean>
    10 </set>
    11 </property>
    12 </bean>
  • 相关阅读:
    Codeforces 547D. Mike and Fish 题解
    Codeforces 521E. Cycling City 题解
    Codeforces 585E. Present for Vitalik the Philatelist 题解
    Codeforces 605E. Intergalaxy Trips 题解
    AGC033D
    第3次作业:卷积神经网络
    Linux下如何编写go语言程序实现菲波拉契数列
    C语言常见典型错误汇总(助教)
    第一次作业:深度学习基础
    数论复习_欧几里得算法与扩展欧几里得算法
  • 原文地址:https://www.cnblogs.com/zxgCoding/p/11580888.html
Copyright © 2011-2022 走看看