SSM consolidation of some cases of stepped pit

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

Could not convert a string in your datasource configured to javax.sql.DataSource objects, resulting in SessionFactory can not be completed, datasource configuration definitely wrong, check the [/WEB-INF/applicationContext.xml] file datasource-related configuration.
<property name = "dataSource" value = "dataSource">
misconfigured, this character string will be written Spring injection, packet type mismatch exception instead
<property name = "dataSource" ref = "dataSource" />

Two, JSP file under WEB-INF ships can then jump back to the controller layer through a request directly to the request file is not get in the path 404 will be reported, and if the direct path address request file, such as <a href = "$ {pageContext.request.contextPath} /Novel.jsp/> then the file is needed at Webapp file folder.


Three, spring mvc type of binding parameters of the conversion, there are three ways:

1. The entity class date formatting comments added

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

Only solve local problems, not solve the fundamental problem

2. Property Editor (generally not a)

Before spring3.1

Controller @InitBinder created by class

/ **
* added one piece of data in the controller layer binding code
* @param WebDataBinder
* /
@InitBinder
public void of an initBinder (WebDataBinder WebDataBinder) throws Exception {
the SimpleDateFormat SimpleDateFormat the SimpleDateFormat new new = ( "the MM-dd-YYYY HH: mm");
simpleDateFormat.setLenient (to false);
webDataBinder.registerCustomEditor (Date.class, the CustomDateEditor new new (SimpleDateFormat, to true));
}
Note: custom type converter must implement PropertyEditor interface or class inheritance PropertyEditorSupport
write a class that extends propertyEditorSupport (implements PropertyEditor) {
void in setAsText public (String text) {
the SimpleDateFormat SimpleDateFormat the SimpleDateFormat new new = ( "-MM-dd YYYY HH: mm");
a Date DATE = simpleDateFormat.parse (text);
this.setValue (DATE);
}
public String getAsTest(){
Date date = (Date)this.getValue();
return this.dateFormat.format(date);
}
}

3. Type Converter Converter (with more, although cumbersome, but cure)

 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 }

 

In the configuration of springMVC.xml

. 1  < : Annotation-Driven MVC Conversion-Service- = "ConversionService" /> 
2  <! - from the date defined Converter -> 
. 3  < the bean ID = "ConversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean " > 
. 4  < Property name =" Converters " > 
. 5  < SET > 
. 6  < the bean class =" edu.kust.utils.DateConvert " > 
. 7  <-! custom date format -> 
8  <property name="pattern" value="yyyy-MM-dd"/>
 9 </bean>
10 </set>
11 </property>
12 </bean>

 

Guess you like

Origin www.cnblogs.com/zxgCoding/p/11580888.html