1. 程式人生 > 其它 >springmvc(3)

springmvc(3)

@RequestMapping

標記請求 URL

很簡單,只需要在相應的方法上新增該註解即可:

    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello")
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("name", helloService.hello("Lang"
)); return mv; }

這裡 @RequestMapping(“/hello”) 表示當請求地址為 /hello 的時候,這個方法會被觸發。其中,地址可以是多個,就是可以多個地址對映到同一個方法。 這個配置,表示 /hello 和 /hello2 都可以訪問到該方法。

    @RequestMapping(value = {"/hello", "/hello2"})
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("hello"
); mv.addObject("name", helloService.hello("Lang")); return mv; }

請求窄化:

同一個專案中,會存在多個介面,例如訂單相關的介面都是 /order/xxx 格式的,使用者相關的介面都是 /user/xxx 格式的。為了方便處理,這裡的字首(就是 /order、/user)可以統一在 Controller 上面處理。

@Controller
@RequestMapping("/user")
public class MyController {
@Autowired private HelloService helloService; @RequestMapping(value = {"/hello", "/hello2"}) public ModelAndView hello() { ModelAndView mv = new ModelAndView("hello"); mv.addObject("name", helloService.hello("Lang")); return mv; } }

當類上加了 @RequestMapping 註解之後,此時,要想訪問到 hello ,地址就應該是 /user/hello 或者 /user/hello2

請求方法限定

預設情況下,使用 @RequestMapping 註解定義好的方法,可以被 GET 請求訪問到,也可以被 POST 請求訪問到,但是 DELETE 請求以及 PUT 請求不可以訪問到。

當然,我們也可以指定具體的訪問方法:

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public ModelAndView postHello() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("name", helloService.hello("Lang"));
        return mv;
    }

通過 @RequestMapping 註解,指定了該介面只能被 POST請求訪問到,此時,該介面就不可以被 GET 以及請求請求訪問到了。強行訪問會報如下錯誤:
在這裡插入圖片描述

當然,限定的方法也可以有多個:

@Controller
@RequestMapping("/user")
public class HelloController {
    @RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
    public ModelAndView hello() {
        return new ModelAndView("hello");
    }
}

返回值

  • 如果是前後端不分的開發,大部分情況下,我們返回 ModelAndView,即資料模型+檢視
@Controller
@RequestMapping("/user")
public class HelloController {
    @RequestMapping("/hello")
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("username", "javaboy");
        return mv;
    }
}

Model 中,放我們的資料,然後在 ModelAndView 中指定檢視名稱。

返回 Void

沒有返回值。沒有返回值,並不一定真的沒有返回值,只是方法的返回值為 void,我們可以通過其他方式給前端返回。實際上,這種方式也可以理解為 Servlet 中的那一套方案。

@RequestMapping("/hello4")
public void hello4(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html;charset=utf-8");
    PrintWriter out = resp.getWriter();
    out.write("hello javaboy!");
    out.flush();
    out.close();
}

這種方式,既可以返回 JSON,也可以返回普通字串。

返回字串

返回邏輯檢視名
前面的 ModelAndView 可以拆分為兩部分,Model 和 View,在 SpringMVC 中,Model 我們可以直接在引數中指定,然後返回值是邏輯檢視名:

@RequestMapping("/hello5")
public String hello5(Model model) {
    model.addAttribute("username", "javaboy");//這是資料模型
    return "hello";//表示去查詢一個名為 hello 的檢視
}

服務端轉發 /跳轉:

@RequestMapping("/hello5")
public String hello5() {
    return "forward:/jsp/hello.jsp";
}

forward 後面跟上跳轉的路徑。

客戶端 重定向、跳轉

@RequestMapping("/hello5")
public String hello5() {
    return "redirect:/user/hello";
}

真的返回一個字串

上面三個返回的字串,都是由特殊含義的,如果一定要返回一個字串,需要額外新增一個注意:@ResponseBody ,這個註解表示當前方法的返回值就是要展示出來返回值,沒有特殊含義。

@RequestMapping("/hello5")
@ResponseBody
public String hello5() {
    return "redirect:/user/hello";
}

上面程式碼表示就是想返回一段內容為 redirect:/user/hello 的字串,他沒有特殊含義。注意,這裡如果單純的返回一箇中文字串,是會亂碼的,可以在 @RequestMapping 中新增 produces 屬性來解決:

@RequestMapping(value = "/hello5",produces = "text/html;charset=utf-8")
@ResponseBody
public String hello5() {
    return "Java 語言程式設計";
}

預設支援的引數型別

  • 預設支援的引數型別,就是可以直接寫在 @RequestMapping 所註解的方法中的引數型別,一共有四類:

  • HttpServletRequest

  • HttpServletResponse

  • HttpSession

  • Model/ModelMap
    這幾個例子可以參考上一小節。

在請求的方法中,預設的引數就是這幾個,如果在方法中,剛好需要這幾個引數,那麼就可以把這幾個引數加入到方法中。

  • 簡單資料型別
    Integer、Boolean、Double 等等簡單資料型別也都是支援的。
    有一個要求,表單中欄位的 name 屬性要和介面中的變數名一一對應,才能對映成功,否則服務端接收不到前端傳來的資料。有一些特殊情況,我們的服務端的介面變數名可能和前端不一致,這個時候我們可以通過 @RequestParam 註解來解決。

@RequestParam
這個註解的的功能主要有三方面:

給變數取別名
設定變數是否必填
給變數設定預設值

@RequestMapping(value = "/doAdd",method = RequestMethod.POST)
@ResponseBody
public void doAdd(@RequestParam("name") String bookname, String author, Double price, Boolean ispublic) {
    System.out.println(bookname);
    System.out.println(author);
    System.out.println(price);
    System.out.println(ispublic);
}

註解中的 “name” 表示給 bookname 這個變數取的別名,也就是說,bookname 將接收前端傳來的 name 這個變數的值。在這個註解中,還可以新增 required 屬性和 defaultValue 屬性,如下:

@RequestMapping(value = "/doAdd",method = RequestMethod.POST)
@ResponseBody
public void doAdd(@RequestParam(value = "name",required = true,defaultValue = "三國演義") String bookname, String author, Double price, Boolean ispublic) {
    System.out.println(bookname);
    System.out.println(author);
    System.out.println(price);
    System.out.println(ispublic);
}

required 屬性預設為 true,即只要添加了 @RequestParam 註解,這個引數預設就是必填的,如果不填,請求無法提交,會報 400 錯誤,如果這個引數不是必填項,可以手動把 required 屬性設定為 false。但是,如果同時設定了 defaultValue,這個時候,前端不傳該引數到後端,即使 required 屬性為 true,它也不會報錯。

  • 實體類
    引數除了是簡單資料型別之外,也可以是實體類。實際上,在開發中,大部分情況下,都是實體類。

還是上面的例子,我們改用一個 Book 物件來接收前端傳來的資料:

public class Book {
    private String name;
    private String author;
    private Double price;
    private Boolean ispublic;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", ispublic=" + ispublic +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Boolean getIspublic() {
        return ispublic;
    }

    public void setIspublic(Boolean ispublic) {
        this.ispublic = ispublic;
    }
}

服務端接收資料方式如下:

@RequestMapping(value = "/doAdd",method = RequestMethod.POST)
@ResponseBody
public void doAdd(Book book) {
    System.out.println(book);
}

自定義引數繫結

前面的轉換,都是系統自動轉換的,這種轉換僅限於基本資料型別。特殊的資料型別,系統無法自動轉換,例如日期。例如前端傳一個日期到後端,後端不是用字串接收,而是使用一個 Date 物件接收,這個時候就會出現引數型別轉換失敗。這個時候,需要我們手動定義引數型別轉換器,將日期字串手動轉為一個 Date 物件。

@Component
public class DateConverter implements Converter<String, Date> {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public Date convert(String source) {
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

在自定義的引數型別轉換器中,將一個 String 轉為 Date 物件,同時,將這個轉換器註冊為一個 Bean。

接下來,在 SpringMVC 的配置檔案中,配置該 Bean,使之生效。

<mvc:annotation-driven conversion-service="conversionService"/>
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
    <property name="converters">
        <set>
            <ref bean="dateConverter"/>
        </set>
    </property>
</bean>

但是一般我們都是通過物件接收資料

集合類的引數

String 陣列
String 陣列可以直接用陣列去接收,前端傳遞的時候,陣列的傳遞其實就多相同的 key,這種一般用在 checkbox 中較多。

<form action="/doAdd" method="post">
    <table>
        <tr>
            <td>書名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>作者姓名:</td>
            <td><input type="text" name="author.name"></td>
        </tr>
        <tr>
            <td>作者年齡:</td>
            <td><input type="text" name="author.age"></td>
        </tr>
        <tr>
            <td>興趣愛好:</td>
            <td>
                <input type="checkbox" name="favorites" value="足球">足球
                <input type="checkbox" name="favorites" value="籃球">籃球
                <input type="checkbox" name="favorites" value="乒乓球">乒乓球
            </td>
        </tr>
        <tr>
            <td>價格:</td>
            <td><input type="text" name="price"></td>
        </tr>
        <tr>
            <td>是否上架:</td>
            <td>
                <input type="radio" value="true" name="ispublic"><input type="radio" value="false" name="ispublic"></td>
        </tr>
        <tr>
           <td colspan="2">
               <input type="submit" value="新增">
           </td>
        </tr>
    </table>
</form>

@RequestMapping(value = "/doAdd",method = RequestMethod.POST)
@ResponseBody
public void doAdd(Book book,String[] favorites) {
    System.out.println(Arrays.toString(favorites));
    System.out.println(book);
}

注意,前端傳來的陣列物件,服務端不可以使用 List 集合去接收。

如果需要使用 List 集合接收前端傳來的資料,List 集合本身需要放在一個封裝物件中,這個時候,List 中,可以是基本資料型別,也可以是物件。例如有一個班級類,班級裡邊有學生,學生有多個:

public class MyClass {
    private Integer id;
    private List<Student> students;

    @Override
    public String toString() {
        return "MyClass{" +
                "id=" + id +
                ", students=" + students +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}
public class Student {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

<form action="/addclass" method="post">
    <table>
        <tr>
            <td>班級編號:</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td>學生編號:</td>
            <td><input type="text" name="students[0].id"></td>
        </tr>
        <tr>
            <td>學生姓名:</td>
            <td><input type="text" name="students[0].name"></td>
        </tr>
        <tr>
            <td>學生編號:</td>
            <td><input type="text" name="students[1].id"></td>
        </tr>
        <tr>
            <td>學生姓名:</td>
            <td><input type="text" name="students[1].name"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>

@RequestMapping("/addclass")
@ResponseBody
public void addClass(MyClass myClass) {
    System.out.println(myClass);
}

Map

相對於實體類而言,Map 是一種比較靈活的方案,但是,Map 可維護性比較差,因此一般不推薦使用。

例如給上面的班級類新增其他屬性資訊:

<form action="/addclass" method="post">
    <table>
        <tr>
            <td>班級編號:</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td>班級名稱:</td>
            <td><input type="text" name="info['name']"></td>
        </tr>
        <tr>
            <td>班級位置:</td>
            <td><input type="text" name="info['pos']"></td>
        </tr>
        <tr>
            <td>學生編號:</td>
            <td><input type="text" name="students[0].id"></td>
        </tr>
        <tr>
            <td>學生姓名:</td>
            <td><input type="text" name="students[0].name"></td>
        </tr>
        <tr>
            <td>學生編號:</td>
            <td><input type="text" name="students[1].id"></td>
        </tr>
        <tr>
            <td>學生姓名:</td>
            <td><input type="text" name="students[1].name"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>