@ReauestMapping
@ReauestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上,用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
创建一个新项目:设置为web项目
编写web.xml(此配置也几乎不更改)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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-servlet.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>
</web-app>
编写配置文件springmvc-servlet.xml(几乎不会更改此内容)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.serenity.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
综上:项目结构:
编写一个Controller类实现Controller接口:
@Controller
@RequestMapping("/c1")
public class ControllerTest1 {
@RequestMapping("/t1")
public String test1(Model model){
model.addAttribute("msg","ControllerTest1");
return "test";
}
}
编写test.jsp文件:
测试:
RestFul风格
概念
RestFul风格:就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
- 资源:互联网所有的事物都可以被抽象为资源。
- 资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
- 分别对应:添加、删除、修改、查询。
传统方式操作资源:通过不同的参数来实现不同的效果!方法单一,post和get
- http:localhost:8080/queryItem.action?id=1 查询,GET
- http:localhost:8080/saveItem.action 新增,POST
- http:localhost:8080/updateItem.action 更新,POST
- http:localhost:8080/deleteItem.action?id=1 删除,GET或POST
使用ReStFul操作资源:可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同。
- http:localhost:8080/Item/1 查询,GET
- http:localhost:8080/Item 新增,POST
- http:localhost:8080/Item 更新,PUT
- http:localhost:8080/Item/1 删除,DELETE
传统方式测试:
@Controller
public class RestFulController {
//http://localhost:8080/add?a=1&b=2
@RequestMapping("/add")
public String test1(int a, int b, Model model){
int res=a+b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
使用ReStFul:
//http://localhost:8080/add/1/2,使用post时需要表单提交
@PostMapping("/add/{a}/{b}")
//@PathVariable注解让方法参数的值对应绑定到一个URL模板变量上。
public String test2(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","结果1为"+res);
return "test";
}
//http://localhost:8080/add/1/2
@GetMapping("/add/{a}/{b}")
public String test3(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","结果2为"+res);
return "test";
}
表单提交时用Post,导航栏直接输入数字是用Get,最后结果的url都是一样的,但是方法不同。
除了使用注解规定提交的方法,也可以属性设置,如下:
return返回地址时默认使用转发。
需要用重定向时:
return "redirect:/index.jsp"
使用RestFul好处:
- 使路径变得更加简洁
- 获得参数更加方便,框架会自动进行类型转换。
- 通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法.
结果跳转方式
ModelAndView对象,根据View的名称和视图解析器跳转到指定的页面
视图解析器前缀+viewName+视图解析器后缀
一般结果(跳转、重定向)都存放在WEB-INF/jsp下的文件,因为这里用户不可见,如果直接存放在web下用户可见。
ServletAPI,不需要视图解析器。
1、通过HttpServletResponse进行输出。
resp.getWriter().println("Hello,SpringMVC");
2、通过HttpServletResponse实现重定向。
resp.sendRedirect("/index.jsp");
3、通过HttpServletResponse实现转发。
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
SpringMVC实现转发和重定向
此处无需视图解析器
将视图解析器注释掉
转发:
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test1(Model model){
model.addAttribute("msg","ModelTest1");
//转发,
return "/WEB-INF/jsp/test.jsp";
//return "forward:/WEB-INF/jsp/test.jsp";
}
}
重定向:
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test1(Model model){
model.addAttribute("msg","ModelTest1");
//重定向
return "redirect:/index.jsp";
}
}
相对于有视图解析器,无视图解析器时,需要写完整的重新定或转发地址。
数据处理
处理提交数据
1、提交的域名称和处理方法的参数名一致
提交数据:http://localhost:8080/user/t1?name=serenity
实体类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
2、提交的域名称和处理方法的参数名不一致
提交数据:http://localhost:8080/user/t1?username=serenity
3、提交的是一个对象,会匹配User对象中的字段名,名字一致就OK,否者匹配不到
要求提交的表单域和对象的属性名一致,参数使用对象即可。提交数据:http://localhost:8080/user?id=1&name=serenity&age=19
处理办法:
@GetMapping("/t2")
//localhost:8080/user/t1?name=xxx
public String test2(User user, Model model){
System.out.println("接收到前端的参数为"+user);
//将返回的结果传递给前端,使用model传递
model.addAttribute("msg",user);
//跳转视图
return "test";
}
数据显示到前端
法一:通过ModelAndView:可以存储数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。
法二:通过Model:存储数据
法三:通过ModelMap:继承了LinkMap,除了实现了自身的一些方法,同样的继承了LinkedMap的方法和特性。