spring restful实践(二)

本部分继续配置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配置文件方式相比没有什么变化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值