首页 > 代码库 > springMVC

springMVC

SpringMVC

1.使用注解编写Controller类

       ①每个请求处理参数对应一个URL

              @Controller

              public class BbsForumController{

                     @Autowired

                     private BbsForumService bbsForumService;

                    

                     @RequestMapping(“/listAllBoard.do”)

                     public String listAllBoard(){

                            bbsForumService.getAllBoard();

                            System.out.println(“call listAllBoard method”);

                            return “llistBoard”;

                     }

 

                     @RequestMapping(“/listBoardTopic.do”)

                     public String listBoardTopic(int topicId){//该处的topicId需要通过url传递

                            bbsForumService.getBoardTopics(topicId);

                            System.out.println(“call listBoardTopic method”);

                            return “listTopic”;

                     }

              }

              注:/listAllBoard.do的url请求将由listAllBoard()方法处理,而/listBoardTopic.do?

topicId=1方法将由listBoardTopic()处理

       ②一个Controller对应一个url,由请求参数决定请求处理方法

              @Controller

              @RequestMapping(“/bbsForum.do”)

              public class BbsForumController{

                     @Autowired

                     private BbsForumService bbsForumService;

 

                     //如果URL请求中包含“method=listAllBoard”的参数,有本方法进行处理

                     @RequestMapping(params=“method=listAllBoard”)

                     public String listAllBoard(){

                            bbsForumService.getAllBoard();

                            System.out.println(“call listAllBoard method”);

                            return “llistBoard”;

                     }

 

                     //如果URL请求中包括“method= listBoardTopic”的参数,有本方法进行处理

                     @RequestMapping(params=“method=listBoardTopic”)

                     public String listBoardTopic(int topicId){//该处的topicId需要通过url传递

                            bbsForumService.getBoardTopics(topicId);

                            System.out.println(“call listBoardTopic method”);

                            return “listTopic”;

                     }

              }

       ③让请求处理方法处理特定HTTP请求方法

              @Controller

@RequestMapping("/bbtForum.do") 

public class BbtForumController {

 

            @RequestMapping(params = "method=createTopic",method = RequestMethod.POST)

            public String createTopic(){

               System.out.println("call createTopic method.");

        return "createTopic";

    }

}

注:只有当/bbtForum.do?method=createTopic请求以post方法提交时,createTopic()方法才会进行处理

springMVC