码迷,mamicode.com
首页 > 其他好文 > 详细

SSM案例整合踩的一些坑

时间:2019-09-24 21:18:13      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:href   support   一个   row   path   tee   time   led   simple   

一、出现错误: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>

 

SSM案例整合踩的一些坑

标签:href   support   一个   row   path   tee   time   led   simple   

原文地址:https://www.cnblogs.com/zxgCoding/p/11580888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!