资料已经打包放在博客下面(文章末尾)
源码分析
源代码
个人 CSDN 博客:CSDN
点击链接查看:SpringMVC 之永远的 Hello world
@RequestMapping:设置请求映射,把请求和控制层中的方法设置映射关系
@RequestMapping("/hello")
public String hello(){
System.out.println("hello");
return "hello";
}
@Controller
@RequestMapping("/test")
public class ControllerTest {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello");
return "hello";
}
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@RequestMapping(value = "/hello", params= {"username","age!=12"} )
@RequestMapping(value = "/hello", headers= {"Accept-Language=zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"} )
@RequestMapping(value = "/*/ant??/**/testAnt")
public String hello1(){
System.out.println("hello");
return "hello";
}
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过 @PathVariable(“xxx“) 绑定到操作方法的入参中。
@RequestMapping("/testREST/{id}/{username}")
public String testREST(@PathVariable Integer id, @PathVariable String username){
System.out.println("id=" + id +",username="+username);
return "hello";
}
REST:即 Representational State Transfer 。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。
在web.xml中配置
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在 web 目录下创建 rest.jsp 文件
<body>
<a href="testREST/100">测试GET</a>
</body>
@RequestMapping(value = "/testREST/{id}",method = RequestMethod.GET)
public String getUserById(@PathVariable("id") Integer id){
System.out.println("GET,id="+id);
return "success";
}
【rest.jsp】
<form action="testREST" method="post">
<input type="submit" value="测试POST">
</form>
@RequestMapping(value = "/testREST", method = RequestMethod.POST)
public String insertUser(){
System.out.println("POSt");
return "success";
}
【rest.jsp】
<form action="testREST" method="POST">
<input type="hidden" name="_method" value="PUT" />
<input type="submit" value="测试PUT" />
</form>
@RequestMapping(value = "/testREST", method = RequestMethod.POST)
public String insertUser(){
System.out.println("POSt");
return "success";
}
【rest.jsp】
<form action="testREST/1001" method="POST">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="测试DELETE" />
</form>
@RequestMapping(value = "/testREST/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable("id") Integer id){
System.out.println("DELETE, id=" + id);
return "success";
}
注意:测试 PUT 和 DELETE 中 Tomcat 为 8.0 或者以上,则会出现以下报错
解决方式:【 JSP 只允许 GET、POST 或 HEAD】
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
改为
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true"%>
请求参数:请求参数 cookie 信息 请求头信息….
JavaWEB: HttpServletRequest
Request.getParameter(参数名); Request.getParameterMap();
Request.getCookies();
Request.getHeader();
代码示例:
【param.jsp】
<body>
<form action="param" method="post">
username:<input type="text" name="username"><br>
password:<input type="text" name="password"><br>
age:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
【ParamController.java】
@RequestMapping(value = "/param",method = RequestMethod.POST)
public String param(@RequestParam(value = "username",required =false, defaultValue = "xiaoming") String name, String password, String age){
System.out.println("name="+name+",password="+password+",age="+age);
return "success";
}
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String param(@RequestHeader("Accept-Language") String AcceptLanguage){
System.out.println("AcceptLanguage="+AcceptLanguage);
return "success";
}
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String param(@CookieValue(value ="JSESSIONID" ) String JSESSIONID){
System.out.println("JSESSIONID"+JSESSIONID);
return "success";
}
【param.jsp】
<body>
<form action="param" method="post">
username:<input type="text" name="username"><br>
password:<input type="text" name="password"><br>
age:<input type="text" name="age"><br>
province:<input type="text" name="address.province"><br>
city:<input type="text" name="address.city"><br>
county:<input type="text" name="address.county"><br>
<input type="submit" value="提交">
</form>
</body>
需要创建 User 类 和 Address 类
public class User {
private String username;
private String password;
private String age;
private Address address;
....
}
public class Address {
private String province;
private String city;
private String county;
...
}
【ParaController.java】
@RequestMapping(value = "/param",method = RequestMethod.POST)
public String param(User user){
System.out.println(user);
return "success";
}
需要配置字符编码过滤器, 且配置其他过滤器之前,如(HiddenHttpMethodFilter),否则不起作用。
<!-- 配置字符集 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在修改 Tomcat
添加:**-Dfile.encoding=UTF-8**
代码示例:
需要导入
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String param(HttpServletRequest request, HttpServletResponse response){
String username = request.getParameter("username");
System.out.println(username);
return "success";
}
@RequestMapping(value = "/param",method = RequestMethod.POST)
public void param(HttpServletRequest request, HttpServletResponse response, Writer out) throws IOException {
System.out.println("param" + request +"," + response);
out.write("hello,world");
// return "success";
}
代码示例:
@RequestMapping(value = "/param", method = RequestMethod.POST)
public ModelAndView param(){
ModelAndView mav = new ModelAndView();
mav.addObject("username", "root");//往request作用域中放值
mav.setViewName("success");//设置视图名称,实现页面跳转
return mav;
}
【success.jsp】
<body>
<h1>成功</h1>
${requestScope.username}
</body>
第一种方式:
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String param(Map<String, Object> map){
map.put("username", "root"); // 往作用域中放值
return "success";// 返回示图名称
}
第二种方式:
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String param(Model model){
model.addAttribute("username", "张三");// 向作用域中放值
return "success"; // 返回试图的名称
}
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value="1"></property>
</bean>
代码示例:
【redirect.jsp】在 Web 目录下创建
<body>
<a href="redirect">redirect测试</a>
<br>
<a href="forward">forward测试</a>
</body>
【RedirectTest.java】
@Controller
public class RedirectTest {
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect(){
System.out.println("redirect测试");
return "redirect:/index.jsp";
}
@RequestMapping(value = "/forward",method = RequestMethod.GET)
public String forward(){
System.out.println("forward测试");
return "forward:/index.jsp";
}
}
重定向原理:
提取码:pwbr
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有