8.21 SpringMVC快速如門
阿新 • • 發佈:2017-08-21
req ria odi 運行 XML ret 密碼 encoding frame
controller中的代碼
1 package com.neuedu.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod;7 import org.springframework.web.bind.annotation.RequestParam; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 @Controller 11 public class AController { 12 private static String SUCCESS="success"; 13 14
用於測試是否能正常運行 15 @RequestMapping("/sayHello") 16 public String sayHello(){17 return SUCCESS; 18 } 19
測試get請求 20 @RequestMapping(value="/order/{id}" ,method=RequestMethod.GET) 21 public String getOrderById1(@PathVariable(value="id") Integer id){ 22 System.out.println(id); 23 return SUCCESS; 24 } 25
測試post請求 26 @RequestMapping(value="/order/{id}" ,method=RequestMethod.POST) 27 public String getOrderById2(@PathVariable(value="id") Integer id){ 28 System.out.println(id); 29 return SUCCESS; 30 } 31
測試put 32 @RequestMapping(value="/order/{id}" ,method=RequestMethod.PUT) 33 @ResponseBody() 34 public String getOrderById3(@PathVariable(value="id") Integer id){ 35 System.out.println(id); 36 return SUCCESS; 37 } 38
測試delete 39 @RequestMapping(value="/order/{id}" ,method=RequestMethod.DELETE) 40 @ResponseBody() 41 public String getOrderById4(@PathVariable(value="id") Integer id){ 42 System.out.println(id); 43 return SUCCESS; 44 } 45
[email protected] 46 @RequestMapping(value="/testRequstPamarm" ,method=RequestMethod.POST) 47 public String testRequstPamarm(@RequestParam("username") String name,@RequestParam("pwd") String pwd){ 48 System.out.println("用戶名:"+name); 49 System.out.println("密碼:"+pwd); 50 return SUCCESS; 51 } 53 }
jsp代碼 1 <body>
2 <a href="${pageContext.request.contextPath}/sayHello">Hello!</a> 3 <br>
測試get 4 <a href="${pageContext.request.contextPath}/order/1">測試GET</a> 5 <br>
測試post請求 6 <form action="${pageContext.request.contextPath}/order/2" method="post"> 7 <input type="submit" value="提交"> 8 </form> 9 <br>
測試put 10 <form action="${pageContext.request.contextPath}/order/3" method="post"> 11 <input type="hidden" name="_method" value="put"> 12 <input type="submit" value="提交"> 13 </form> 14 <br>
測試delete 15 <form action="${pageContext.request.contextPath}/order/4" method="post"> 16 <input type="hidden" name="_method" value="DELETE"> 17 <input type="submit" value="提交"> 18 </form> 19 <br>
[email protected] 20 <form action="${pageContext.request.contextPath}/testRequstPamarm" method="post"> 21 用戶名:<input type="text" name="username"> 22 <br> 23 密 碼:<input type="text" name="pwd"> 24 <br> 25 <input type="submit" value="提交"> 26 </form> 27 </body>
get可以用超鏈接,post只能用from表單,put和delete要在post基礎上加上<input name="_method" value="">
還要註意put和delete還需要在web.xml中配置過濾器filter,代碼如下:
1 <filter> 2 <filter-name>hiddenHttpMethodFilter</filter-name> 3 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>hiddenHttpMethodFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
[email protected]中加入下面代碼:(註意此代碼要放在過濾器上面)
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>utf-8</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>CharacterEncodingFilter</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>
web-xml中的其他配置:
1 <!-- The front controller of this Spring Web application, responsible for handling all application requests --> 2 <servlet> 3 <servlet-name>springDispatcherServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <!-- <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:springmvc.xml</param-value> 8 </init-param> --> 9 <load-on-startup>1</load-on-startup> 10 </servlet> 11 12 <!-- Map all requests to the DispatcherServlet for handling --> 13 <servlet-mapping> 14 <servlet-name>springDispatcherServlet</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping>
springDispatcherServlet-servlet.xml中的配置:
1 <context:component-scan base-package="com.neuedu.controller"></context:component-scan> 2 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 3 <property name="prefix" value="/WEB-INF/views/"></property> 4 <property name="suffix" value=".jsp"></property> 5 </bean>
8.21 SpringMVC快速如門