1 <context:component-scan base-package="com.imethsoft.server.*" /> 2 <!-- 开启注解 --> 3 <mvc:annotation-driven /> 4 5 <!-- 静态资源(js/image)的访问 --> 6 <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" /> 7 <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" /> 8 <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" /> 9 10 <!-- 定义视图解析器 --> 11 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 12 <property name="prefix" value="/"></property> 13 <property name="suffix" value=".jsp"></property> 14 </bean>
在web.xml配置如下即可:第一种方式:文件放在WEB-INF下面,取名为springMVC-servlet.xml
1 <!-- 配置SpringMVC --> 2 <servlet> 3 <servlet-name>springMVC</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <load-on-startup>1</load-on-startup> 6 </servlet> 7 8 <servlet-mapping> 9 <servlet-name>springMVC</servlet-name> 10 <url-pattern>/</url-pattern> 11 </servlet-mapping>
1 <!-- 配置SpringMVC --> 2 <servlet> 3 <servlet-name>springMVC</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:applicationContext-servlet.xml</param-value> 9 </init-param> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>springMVC</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping>
1 <!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml --> 2 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 3 <property name="order" value="1" /> 4 <!-- 在没有扩展名时即: "/blog/1" 时的默认展现形式 --> 5 <property name="defaultContentType" value="text/html" /> 6 <!-- 是否启用扩展名支持,默认就是true --> 7 <property name="favorPathExtension" value="true"></property> 8 9 <!-- 设置为true以忽略对Accept Header的支持 --> 10 <property name="ignoreAcceptHeader" value="true" /> 11 <!-- 用于开启 /blog/123?format=json 的支持,一旦开启就要加format参数 --> 12 <property name="favorParameter" value="false" /> 13 14 <!-- 扩展名至mimeType的映射,即 /blog.json => application/json --> 15 <property name="mediaTypes"> 16 <map> 17 <entry key="xml" value="application/xml" /> 18 <entry key="json" value="application/json" /> 19 </map> 20 </property> 21 22 <property name="defaultViews"> 23 <list> 24 <!-- for application/json --> 25 <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> 26 27 <!-- for application/xml --> 28 <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 29 <property name="marshaller"> 30 <bean class="org.springframework.oxm.xstream.XStreamMarshaller" /> 31 </property> 32 </bean> 33 </list> 34 </property> 35 </bean>
1 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 2 <property name="order" value="2" /> 3 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 4 <property name="prefix" value="/"></property> 5 <property name="suffix" value=".jsp"></property> 6 </bean>
1 @RequestMapping(value="/getById",method=RequestMethod.GET) 2 public ModelAndView getById(Model model, HttpServletRequest request){ 3 4 User user = new User(); 5 user.setUsername("aaa"); 6 user.setPassword("bbb"); 7 8 Map<String, Object> map = new HashMap<String, Object>(); 9 map.put("row", user); 10 return new ModelAndView("getById", map); 11 }
1 <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 2 <property name="defaultErrorView" value="error500"></property> 3 <property name="defaultStatusCode" value="500"></property> 4 <property name="statusCodes"> 5 <props> 6 <prop key="error500">500</prop> <!-- error.jsp --> 7 <prop key="error404">404</prop> <!-- error1.jsp --> 8 </props> 9 </property> 10 <property name="exceptionMappings"> 11 <props> 12 <!-- 这里你可以根据需要定义N多个错误异常转发 --> 13 <!-- 数据库错误(dbError.jsp) --> 14 <prop key="java.sql.SQLException">dbError</prop> 15 <!-- 参数绑定错误(如:必须参数没传递)(bizError.jsp) --> 16 <prop key="org.springframework.web.bind.ServletRequestBindingException">bizError</prop> 17 <!-- 参数错误(bizError.jsp) --> 18 <prop key="java.lang.IllegalArgumentException">bizError</prop> 19 <!-- 参数类型有误(bizError.jsp) --> 20 <prop key="org.springframework.validation.BindException">bizError</prop> 21 <!-- 其他错误为‘未定义错误‘(unknowError.jsp) --> 22 <prop key="java.lang.Exception">unknowError</prop> 23 </props> 24 </property> 25 </bean>
1 <error-page> 2 <error-code>500</error-code> 3 <location>/error500.jsp</location> 4 </error-page> 5 6 <error-page> 7 <error-code>404</error-code> 8 <location>/error404.jsp</location> 9 </error-page>
这样可以保证在springmvc没有拦截到的时候可以跳转到web.xml中进行拦截错误处理
1 <!-- 配置默认的注解映射 --> 2 <mvc:annotation-driven> 3 <mvc:message-converters> 4 <ref bean="stringHttpMessageConverter" /> 5 <ref bean="fastJsonHttpMessageConverter" /> 6 </mvc:message-converters> 7 </mvc:annotation-driven> 8 9 <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> 10 <!-- 避免出现乱码 --> 11 <property name="supportedMediaTypes"> 12 <list> 13 <value>text/plain;charSet=UTF-8</value> 14 </list> 15 </property> 16 </bean> 17 18 <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 19 <property name="supportedMediaTypes"> 20 <list> 21 <value>application/json;charset=UTF-8</value> 22 <value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 --> 23 </list> 24 </property> 25 <property name="features"> 26 <util:list> 27 <value>WriteMapNullValue</value> 28 <value>QuoteFieldNames</value> 29 <value>WriteDateUseDateFormat</value> 30 </util:list> 31 </property> 32 </bean>
原文:http://www.cnblogs.com/Nonnetta/p/5060758.html