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: /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","/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乱码问题