本部分继续配置spring restful能正常工作需要的配置步骤
1.中文post
这部分还是使用xml配置方式,可以兼容,使用java代码的方式目前还不会
2.apache tiles
首先在pom.xml引入dependency
<!-- Apache Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>2.2.2</version>
</dependency>
在WebConfig.java加入
/**
* ViewResolver configuration required to work with Tiles2-based views.
*/
@Bean
public ViewResolver tilesviewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(TilesView.class);
return viewResolver;
}
/**
* Configures Tiles at application startup.
*/
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] {
"/WEB-INF/layouts/tiles.xml",
"/WEB-INF/views/**/tiles.xml"
});
configurer.setCheckRefresh(true);
return configurer;
}
tiles文件配置可以根据自己需要任意定制,对应的目录和文件必须存在,否则会出错
tiles文件的编写和jsp文件的编写和xml配置方式下完全一样
3.resource配置
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
4.i18n
在WebConfig.java加入
/**
* Messages to support internationalization/localization.
*/
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages/messages_cn");
return messageSource;
}
5.mybatis
这里遇到了困难,最后发现只能通过java config和xml配置相结合的方式,方式如下:
首先去掉:
<!-- Java-based annotation-driven Spring container definition -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
然后修改:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/services-config.xml</param-value>
<!-- <param-value>com.yunzu.testmybatis.config</param-value> -->
</context-param>
这里使用services-config.xml文件指定xml配置和java config结合,例子如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="servlet-context.xml"/> <!-- MyBatis config for persistence jar -->
<bean class="com.yunzu.testmybatis.config.WebConfig"/> <!-- Java web config -->
<bean class="com.yunzu.testmybatis.config.ComponentConfig"/>
</beans>
import的部分是xml配置文件,和原来方式没有什么不同,而bean指定的class低java config,和原来java config的也没什么不同
参考:
http://mybatis-user.963551.n3.nabble.com/Spring3-1-issue-when-using-Java-Config-multiple-datasources-td3773064.html
6.json支持
这个和xml配置文件方式相比没有什么变化