spring mvc的 rest风格

时间:2022-12-10 20:08:48

最近带的团队项目转到spring mvc,彻底的离开了struts的MVC,感觉spring mvc要比struts mvc好用很多,虽然说struts 一直从1.0+一直用到2.3.4。

这里项目管理用maven,一直使用了4年maven,感觉非常好用。


好了,废话少说,先贴出配置文件吧

1、项目依赖。

		<!-- logback dependencies,日志需要 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>${logback.version}</version>
		</dependency>

		<dependency>
			<groupId>org.logback-extensions</groupId>
			<artifactId>logback-ext-spring</artifactId>
			<version>0.1.1</version>
		</dependency>

		<!-- commons-logging replace dependencies -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.6.4</version>
		</dependency>

spring 配置需要

		<!-- spring dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion><!-- 排除底层日志,这个存在会影响我们使用logback的日志输出,使用 jcl-over-slf4j 代替 -->
			</exclusions>
		</dependency> <!--包括了基本的spring依赖 -->

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency> <!--如果你需要数据库支持就要, 不然就不需要 -->



2、web.xml 配置:


<!-- logback -->
	<context-param>
		<param-name>logbackConfigLocation</param-name>
		<param-value>classpath:logback.xml</param-value>
	</context-param>

	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value><!--你的spring.xml配置文件的路径 --></param-value>
	</context-param>

	<!-- Logback the root web application before spring servlet init -->
	<listener>
		<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class><!-- logback的监听-->
	</listener>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- HttpServletRequest Listener -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

	<!-- Encoding filter spring -->
	<filter>
		<filter-name>character</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<!-- Spring mvc DispatcherServlet spring mvc 配置,我们不需要根据名字获取配置文件,直接 init-param -->
	<servlet>
		<servlet-name>Spring servlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>WEB-INF/spring/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
 
 
	<!-- Encoding filter -->
	<filter-mapping>
		<filter-name>character</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet-mapping>
		<servlet-name>Spring servlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


 


3、spring mvc的核心配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
	default-autowire="byName">

	<!-- welcome page -->
	<mvc:view-controller path="/" view-name="login" />

	<!-- base scan -->
	<context:component-scan base-package="控制层的基础包路径" />

	<!-- static resources -->
	<mvc:resources mapping="/static/**" location="/static/***" cache-period="36000" />

	<!-- <mvc:annotation-driven /> -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<bean class="org.springframework.http.MediaType"><!--防止乱码 -->
								<constructor-arg index="0" value="text" />
								<constructor-arg index="1" value="plain" />
								<constructor-arg index="2" value="UTF-8" />
							</bean>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

	<!-- 拦截请求即可, -->
	<!-- 如果用spring mvc:resources mvc:default-servlet-handler时候 -->
	<!-- 可能会拦截静态文件 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 拦截权限 -->
			<mvc:mapping path="/请求/**" />
			<bean class="com.xx.xx.util.AccessInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/template/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>


4、demo控制代码:


@Controller
@RequestMapping("test")
public class TestController {
	/**
	 * 重新定向到列表字符常量
	 */
	private static final String LIST = "redirect:/test/list";

	/**
	 * 列表信息
	 * 
	 * @param curPage 当前页
	 * @param pageSize 每页记录
	 * @param queryBean 查询对象
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView list(Integer curPage, Integer pageSize, QueryBean queryBean, boolean b, String json) {
		ModelAndView modelAndView = new ModelAndView();
		// ...dosomething
		return modelAndView;
	}

	/**
	 * 进入新增部门信息页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public ModelAndView newAdd() {
		return new ModelAndView("m/testAdd").addObject("testVO", new TestVO());
	}

	/**
	 * 新增部门信息
	 * 
	 * @param testVO
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public ModelAndView add(TestVO testVO, BindingResult bindingResult) {
		ModelAndView andView = new ModelAndView();
		return andView;
	}

	/**
	 * 进入部门编辑页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public ModelAndView newEdit(@PathVariable Integer id, @RequestParam Map<String, String> map) {
		ModelAndView andView = new ModelAndView();
		return andView;
	}

	/**
	 * JSON测试
	 */
	// TODO注意
	@ResponseBody
	@RequestMapping(value = "testjson", method = RequestMethod.GET)
	public String testJson() {
		return "a";
	}

}


到此基本上完成了