亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springMVC rest風格

發布時間:2020-07-12 08:50:32 來源:網絡 閱讀:289 作者:乾坤刀 欄目:開發技術

1.dispatcherServlet的配置

	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc-ftl.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
注:<url-pattern>/</url-pattern>不能指定后綴名


2. 使servlet可以操作PUT和DELETE

    <!-- 支持http的PUT和DELTE操作 --> 
    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


3.controller示例

@Controller
@RequestMapping("/user/")
public class UserController 
{

	@RequestMapping(value="index", method=RequestMethod.GET, produces={"application/json;charset=UTF-8"})
	public ModelAndView index()
	{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("home");
		return modelAndView;
	}
	
	@RequestMapping(value="user/{id}", method=RequestMethod.GET, produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public User getUser(@PathVariable("id") String userId)
	{
		System.err.println("查詢用戶信息:" + userId);
		return new UserBuilder().setUserId(userId).setUserName("測試").toUser();
	}
	
	@RequestMapping(value="user", method=RequestMethod.POST, produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public User addUser(String userId, String userName, int userAge)
	{
		System.err.println("新增用戶信息:" + userId);
		return new UserBuilder().setUserId(userId).setUserName(userName).setUserAge(userAge).toUser();
	}
	
	@RequestMapping(value="user", method=RequestMethod.PUT, produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public User editUser(String userId, String userName, int userAge)
	{
		System.err.println("修改用戶信息:" + userId);
		return new UserBuilder().setUserId(userId).setUserName(userName).setUserAge(userAge).toUser();
	}
	
	@RequestMapping(value="user/{id}", method=RequestMethod.DELETE, produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public User deleteUser(@PathVariable("id") String userId)
	{
		System.err.println("刪除用戶信息:" + userId);
		return new UserBuilder().setUserId(userId).toUser();
	}
}


4.js示例

   <script type="text/javascript">	
   		function fnQuery(){
   		    $.ajax({
	             type: 'GET',
	             url: '${request.contextPath}/user/user/111',
	             async: true,
	             data: {},
	             dataType: "json",
	             success: function(data){
	                alert(JSON.stringify(data));
	             },
	             error:function(msg){
	                alert(JSON.stringify(msg));
	             }
	         });
	     }
   		function fnAdd(){
   		    $.ajax({
	             type: 'POST',
	             url: '${request.contextPath}/user/user',
	             async: true,
	             data: {
	                'userId':'123',
	                'userName':'測試測試測試測試',
	                'userAge':88,
	             },
	             dataType: "json",
	             success: function(data){
	                alert(JSON.stringify(data));
	             },
	             error:function(msg){
	                alert(JSON.stringify(msg));
	             }
	         });
	     }
   		function fnEdit(){
   		    $.ajax({
	             type: 'POST',
	             url: '${request.contextPath}/user/user',
	             async: true,
	             data: {
	                _method:'PUT',
	                'userId':'55555',
	                'userName':'修改試測試測試',
	                'userAge':48,
	             },
	             dataType: "json",
	             success: function(data){
	                alert(JSON.stringify(data));
	             },
	             error:function(msg){
	                alert(JSON.stringify(msg));
	             }
	         });
	     }
	     
   		function fnDelete(){
   		    $.ajax({
	             type: 'POST',
	             url: '${request.contextPath}/user/user/111',
	             async: true,
	             data: {
	                _method:'DELETE',
	             },
	             dataType: "json",
	             success: function(data){
	                alert(JSON.stringify(data));
	             },
	             error:function(msg){
	                alert(JSON.stringify(msg));
	             }
	         });
	     }
	</script>


注:PUT與DELETE的特殊處理,需要在參數中增加_method.


參考地址:http://www.cnblogs.com/qixiaoyizhan/p/5864233.html


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

呼伦贝尔市| 石渠县| 定襄县| 承德市| 白山市| 四会市| 乌兰浩特市| 旬阳县| 靖西县| 永胜县| 莎车县| 怀安县| 浦东新区| 虞城县| 安岳县| 赫章县| 南宁市| 四川省| 德令哈市| 洛宁县| 竹山县| 平度市| 广昌县| 萍乡市| 涞水县| 榆林市| 宜州市| 汾西县| 岫岩| 南宁市| 灌云县| 五寨县| 翁源县| 哈尔滨市| 留坝县| 丰镇市| 许昌市| 二手房| 乐安县| 揭东县| 正安县|