springMvc6-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: geyao
Date: 2019/11/6
Time: 19:57
To 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) { // 参数中传入Model
model.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乱码问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南