SpringMVC補充

1.URL模板映射  2.@requestParam參數傳遞  3.轉發和重定向  4.json數據處理

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <filter>
      <filter-name>characterFilter</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>characterFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
      <servlet-name>springmvc</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>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

URL模板映射

普通帶參:localhost:8080/springmvcpj/user/deleteUser.do?id=2javascript

REST風格:localhost:8080/springmvcpj/user/deleteUser/2html

@requestParam參數傳遞

value的值必須和表單input的name屬性值同樣
required:true 必須傳參,不然就報400錯誤java

user.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="${pageContext.request.contextPath}/re/delUser/3/admin.do">刪除</a><br/>
    
    <form action="${pageContext.request.contextPath}/re/addUser.do">
        用戶名:<input name="name"/><br/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

cn.bdqn.controller.RestController.java

package cn.bdqn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/re")
public class RestController {
    @RequestMapping("/delUser/{uid}/{uname}")
    public String delUser(@PathVariable Integer uid,@PathVariable String uname){
        System.out.println("刪除"+uid+"的用戶");
        System.out.println(uname);
        return "index";
    }
    @RequestMapping("addUser")
    //value的值必須和表單input的name屬性值同樣
    public String addUser(@RequestParam(value="name",required=false) String uname){  //required屬性 true 必須傳參,不然就報400錯誤
        System.out.println(uname);
        return "index";
    }
}

index.jsp

  <body> ${age} </body>

轉發和重定向

springMVC跳轉頁面默認是轉發jquery

轉發也可寫做 "forward:/index.jsp";web

重定向:

@RequestMapping("/setAtr")
public String setAtr(HttpServletRequest request){
request.setAttribute("age", 15);
return "redirect:/index.jsp";  //重定向至index.jsp頁面
}

json數據處理

spingmvc添加json支持(jackson) jar文件ajax

jackson-core-asl-1.9.11.jarspring

jackson-mapper-asl-1.9.11.jarjson

@RequestBody接受前臺json數據,把json數據自動封裝javaBean。@responseBody把後臺pojo轉換json對象,返回到頁面。mvc

ajaxJson.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
</head>
<body>
    <h4>發送json和響應json</h4>
    <input type="button" id="btn" value="點擊"/>
    
    <script type="text/javascript">
        $(function(){
            var jsonData = JSON.stringify({"uid":101,"uname":"李四","age":15}); //JSON.stringify 將json對象轉爲字符串形式
            $("#btn").click(function(){
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/json/getJsonBean.do",
                    contentType:"application/json;charset=UTF-8",
                    data:jsonData,
                    success:function(repData){
                        alert(repData);
                    }
                });
            });
        });
    </script>
</body>
</html>

/getJsonBean.do

@Controller
@RequestMapping("/json")
public class JsonController {
    @RequestMapping("getJsonBean")
    //@responseBody把後臺pojo轉換json對象,返回到頁面
    public @ResponseBody User getJsonBean(@RequestBody User user){ //@RequestBody接受前臺json數據,把json數據自動封裝javaBean
        System.out.println(user);
        return user;
    }
相關文章
相關標籤/搜索