1. 程式人生 > >SpringMVC 註解開發

SpringMVC 註解開發

先在一個包中建立一個類,然後再配置sprringmvacontroller.xml並連結到該類。

<context:component-scan base-package="cn.happy.controllerreturn"></context:component-scan>
 註解開發所建立的類及其方法

  萬用字元:是一種符號,不是精確匹配,而是用來代替

                ** 代表任意級別目錄,或者沒有目錄

package cn.happy.Controler;
import org.springframework.stereotype.Controller
; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by java on 2017/8/18. */ @Controller @RequestMapping("/user") public class
HappyController { @RequestMapping("/first") public String doFirst(){ return "/WEB-INF/index.jsp"; } @RequestMapping("/second") public String doSecond(){ return "/WEB-INF/insert.jsp"; } @RequestMapping("/*thtird") public String doThird(){ return "/WEB-INF/delete.jsp"
; } @RequestMapping("/**/fourth") public String doFourth(){ return "/WEB-INF/insert.jsp"; } @RequestMapping("/*/six") public String doSix(){ return "/WEB-INF/insert.jsp"; } //路徑變數 @RequestMapping(value = "/{name}/{age}/first.do",method = {RequestMethod.POST,RequestMethod.GET}) public String doFirstcoller(@PathVariable() String name, @PathVariable() String age){ System.out.println(name); System.out.println(age); return "/WEB-INF/index.jsp"; } @RequestMapping(value = "/first.do",method = {RequestMethod.POST,RequestMethod.GET}) public String doFirst(String name, Model model){ System.out.println("name"+name); model.addAttribute("name"+name); return "index"; } }
 請求中方式的定義:
   method屬性的取值為Requestmethod,是一個列舉常量
   先建立一個UserInfo 實體類並寫上屬性,並進行封裝:   
   域屬性:一個類中變數的型別是另外一個自定義型別。
public class UserInfo {
    private String name;
    private  Book book;
    private List<Book> books;
    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {   //集合屬性
        this.books = books;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
並且再建立Book的實體類,也寫上一個屬性,將其進行封裝
public class Book {
    private String bookname;

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
}
之後建立ArgementController類
@Controller
public class ArgementController {
    @RequestMapping("/login2")     //限定請求方式
        public String login2(UserInfo  info){
        System.out.println(info.getName());
        return "/WEB-INF/delete.jsp";
        }

    @RequestMapping("/AreArggement")
    public String login4(UserInfo  info){
        System.out.println(info.getName()+"\t"+info.getBook().getBookname());
        return "/WEB-INF/delete.jsp";
    }

//@RequestParam  校正引數名稱

    @RequestMapping("/rightArggument")
    public String  logg3(@RequestParam("name") String uname2){
        System.out.println(uname2);
        return "/WEB-INF/delete.jsp";
    }

@RequestMapping("/listArgument")
public String listlogin5(UserInfo info){
    System.out.println(info.getName()+"\t"+info.getBooks().get(0).getBookname());
        return "/WEB-INF/delete.jsp";
}

}
Web.xml
亂碼解決forceEncoding
<!--javaWeb 三大件 servlet Filter listener -->
<filter>
    <filter-name>CharactorEncodingFilter</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>
    
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharactorEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>