<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 開啓SpringMVC註解模式 簡化配置: 1. 自動註冊 DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter 2. 提供一系列默認支持:數據綁定、日期格式轉換,xml&json支持 --> <mvc:annotation-driven> </mvc:annotation-driven> <!-- servlet-mapping映射路徑:/ 靜態資源默認servlet配置 1. 加入對靜態資源的處理:js,css,gif 2. 容許使用「/」作總體映射 --> <mvc:default-servlet-handler> </mvc:default-servlet-handler> <!-- 配置JSP,顯示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"> </property> <property name="prefix" value="/WEB-INF/jsp"> </property> <property name="suffix" value=".jsp"> </property> </bean> <!-- 掃描web相關的bean --> <context:component-scan base-package="com.mbq.seckill.web"> </context:component-scan> </beans>
@RequestMapping:css
springmvc的form表單標籤,支持自動回顯java
請求轉發與重定向web
使用@RequestParam 綁定請求參數值
使用@RequestHeader綁定請求報頭的屬性值
使用@CookieValue綁定請求中的Cookie值ajax
在web.xml中配置HiddenHttpMethodFilter過濾器,將普通的post請求轉換爲指定的put或delete請求(post請求中需有參數:_method=put)
1. 帶{}佔位符的URL
2. @PathVariablespring
ajax直接發送put/delete請求時,後臺會獲取不到請求中的參數,此爲tomcat內部編碼的問題,若是不是get/post請求,則直接返回。json
Controller亂碼問題解決:在web.xml中配置一個過濾器spring-mvc
<filter> <filter-name>CharacterEncodingFilter</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> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
數據類型轉換
數據格式化:日期類型:在屬性上添加註解@DateTimeFormat(pattern="yyyy-MM-dd")tomcat
@NotNull private String ename; @RequestMapping(value="/save", method=RequestMethod.POST) @ResponseBody public Msg save(@Valid Employee emp, BindingResult result) { if(result.hasErrors()) { List<FieldError> fieldErrors = result.getFieldErrors(); Map<String, Object> map = new HashMap<>(); for(FieldError fieldError : fieldErrors) { //錯誤字段名,錯誤信息 map.put(fieldError.getField(), fieldError.getDefaultMessage()); } return new Msg().fail().add("errorField", map); } employeeService.save(emp); return new Msg().success(); }
ajax返回json:@ResponseBody,須要jackson的支持(將java對象轉換爲json對象),能夠封裝一個通用的Json對象類restful
配置視圖解析控制器:<mvc:view controller ...>,直接轉發頁面,不通過Handlermvc
1.首先須要進行配置
<!-- 必定要寫id,對應文件上傳的攔截器 --> <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="2048000"></property> </bean>
2.使用MultipartFile接收上傳的文件,transferTo()方法實現文件上傳操做
繼承HandlerInterceptorAdapter
實現HandlerInterceptor接口
在配置文件中進行配置
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/admin/**"/> <bean class="com.ma.shop.interceptor.AutoLoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
@RunWith(SpringRunner.class) @WebAppConfiguration @ContextConfiguration({ "classpath:spring-web.xml", "classpath:spring-service.xml", "classpath:spring-dao.xml", }) public class ControllerTest { @Autowired private WebApplicationContext context; //虛擬MVC請求,獲取處理結果 private MockMvc mockMvc; @Before public void initMockMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPage() throws Exception{ //模擬請求添加參數並獲取返回值 MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emp/list").param("pageNo", "1")).andReturn(); MockHttpServletRequest request = result.getRequest(); Employee emp = (Employee) request.getAttribute("emp"); } }