spring restful实践(一)

最近学习spring greenhouse项目,发现是用restful方式,和原来使用xml配置文件的方式差别比较大,写下来备忘。

1.创建spring mvc项目

为了方便我使用了eclipse spring插件直接建了个项目

2.改造web.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>
修改contextConfigLocation,原来对应的是配置文件,现在对应的是配置目录:

	<!-- 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/root-context.xml</param-value> -->
		<param-value>com.yunzu.oauthtest.config</param-value>
	</context-param>
另外servlet的配置修改成以下方式:

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> -->
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

2.pom.xml修改

添加cglib

                 <dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency> 

3.配置目录修改

src/main/java下创建包com.yunzu.oauthtest.config,对应到上面的contextConfigLocation

然后创建ComponentConfig类,内容如下:

package com.yunzu.oauthtest.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@ComponentScan(basePackages="com.yunzu.oauthtest")
public class ComponentConfig {
	/**
	 * Properties to support the 'embedded' mode of operation.
	 */
	@Configuration
	static class Embedded {
	}
}
重点是两个注解,相当于xml配置的<context:component-scan base-package="com.yunzu.oauthtest" />
有了这些项目就可以运行起来了,但却不能访问到任何可用的页面,这是因为还没有配置mvc

为配置mvc,需要创建WebConfig类,最简单配置如下:

package com.yunzu.oauthtest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
public class WebConfig  extends WebMvcConfigurerAdapter {
	private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views/";
    private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
   
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);
        viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);

        return viewResolver;
    }
}
这里实现了view resolver,有了这个配置,就可以正常的看到页面了
注意要extends WebMvcConfigurerAdapter,这样才能由系统调用addInterceptors等接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值