spring mvc学习(6):springMVC的常见注解
生活随笔
收集整理的這篇文章主要介紹了
spring mvc学习(6):springMVC的常见注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1@RequestMapping的位置
可以在類名或者方法名之前
或者同時加在兩個位置
最終的路徑是兩個位置路徑的組合
value是默認的名稱,可以省略,如果有其他參數,就不能省略
如下配置的訪問路徑:協議://主機://端口/虛擬路徑/hello/world
package com.geyao.springmvc.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/hello") public class HelloWorld {@RequestMapping(value="/world")public String helloworld(){System.out.println("hello eorld");return "helloworld";} }2 @RequestMapping的請求方式
GET形式?
@RequestMapping(value = "/helloworld",method = RequestMethod.GET)Post形式
@RequestMapping(value = "/helloworld",method = RequestMethod.POST)如果不指定merhod,那么可以接受任何請求
請求方式不對,報405錯誤
3處理請求參數
表單的controller和form
<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/6Time: 19:57To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> <form action="hi" method="post"><label for="username">用戶名<input type="text" id="username" name="username"></label><label for="password">密碼<input type="text" id="password" name="password"></label><button>登錄</button> </form> </body> </html> package wormday.springmvc.helloworld;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; // 這里導入了一個Model類 import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;@Controller @RequestMapping("/hi") public class HiController {@RequestMapping("/say")public String say(Model model) { // 參數中傳入Modelmodel.addAttribute("name","wormday"); // 指定Model的值model.addAttribute("url","http://www.cnblogs.com/wormday/p/8435617.html"); // 指定Model的值return "say";}@RequestMapping("/loginForm")public String loginForm(){return "login";}@RequestMapping("/hi")public String loginFor(){return "hi";}@RequestMapping(value = "/login",method = RequestMethod.POST)public String loginXS(){// System.out.println("執行登錄");//System.out.println("username"+username);//System.out.println("password"+password);return "redirect:hi";} }提交表單的controller
@RequestMapping(value = "/login",method = RequestMethod.POST)public String loginXS(String username,String password){System.out.println("執行登錄");System.out.println("username"+username);System.out.println("password"+password);return "redirect:hi";} }4請求轉發和跳轉
5解決post亂碼問題
6解決get亂碼問題
總結
以上是生活随笔為你收集整理的spring mvc学习(6):springMVC的常见注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vista下载_Vista和视图在游戏设
- 下一篇: spring mvc学习(15)Refe