springmvc
1. web.xml
<!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) --> <!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) --> <!-- 注意xml 文件 节点的顺序问题,有时候可能报错 --> <!-- 不使用listener监听器来加载Spring 的配置,改用DispatcherServlet来加载spring的配置,简单 --> <!-- for spring mvc --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
spring -mvc.xml
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 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"> <!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="com.diancai.webservice" ><!-- 多个包用逗号隔开 --> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> </context:component-scan> <!-- 默认的注解支持 --> <mvc:annotation-driven/> <!-- 视图解释类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-IF/jsp/"></property> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> </bean> <!-- 拦截器 --> <mvc:interceptors> <bean class="com.diancai.inteceptor.MyInterceptor"></bean><!-- 想当于 总拦截器,拦截handlerMapping --> <!-- <mvc:interceptor>只拦截匹配的url <mvc:mapping path="/user/*"/> <bean class="com.diancai.inteceptor.MyInteceptor"></bean> </mvc:interceptor> --> <!-- 第三种:如果使用了<mvc:annotation-driven />, 它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter 这两个bean, 所以就没有机会再给它注入interceptors属性,就无法指定拦截器。 当然我们可以通过人工配置上面的两个Bean,不使用 <mvc:annotation-driven />,就可以 给interceptors属性 注入拦截器了。 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.mvc.MyInteceptor"></bean> </list> </property> </bean> --> </mvc:interceptors> <!-- 将无法mapping到Controller的path交给default servlet handler处理,如js,css,img等静态文件 。--> <mvc:default-servlet-handler/> </beans>
package com.diancai.webservice.rest.restaurant; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.diancai.entity.DcRestaurant; import com.diancai.service.dcrestaurant.DcRestaurantService; /** * 饭店 controller * @author Administrator * @Controller --- spring-context jar @requestMapping---spring-web jar * spring mvc 方式 */ @Controller @RequestMapping(value="/restaurant") public class RestaurantController { /*@Autowired private DcRestaurantService dcRestaurantService;*/ /** * 获得restaurant list */ @RequestMapping(value="/restaurantList" ,method =RequestMethod.GET ) @ResponseBody public List<DcRestaurant> getRestlist(){ //List<DcRestaurant> list=dcRestaurantService.getRestaurantList(); //return list; return null; } /** * 测试 */ @RequestMapping(value="/test" ,method =RequestMethod.GET ) @ResponseBody public ModelAndView text(){ ModelAndView model=new ModelAndView(); model.setViewName("redirect:/index.jsp"); return model;//返回到视图 } }