package org.westos.pojo;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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="org.westos.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>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${message}
返回的是ModelAndView对象
@Controller
public class SpringMVCTest {
//使用ModelAndView
@RequestMapping("t1")
public ModelAndView test1(HttpServletRequest request, HttpServletResponse response){
ModelAndView mv = new ModelAndView();
mv.addObject("message","test1");
mv.setViewName("hello");
return mv;
}
}
只有controller层的跳转方式不同
//使用ServletAPI
@RequestMapping("/t2")
public void test2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("message","test2");
request.getRequestDispatcher("WEB-INF/jsp/hello.jsp").forward(request,response);
}
使用forward转发方式,redirect重定向方式不能携带参数,只能实现无参数跳转
//SpringMVC不带视图解析器
@RequestMapping("t3")
public String test3(HttpServletRequest request){
request.setAttribute("message","test3");
return "forward:WEB-INF/jsp/hello.jsp";
}
//SpringMVC带试图带视图解析器
@RequestMapping("t4")
public String test4(Model model){
model.addAttribute("message","test4");
return "hello";
}
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制
CRUD 动作 | HTTP 方法 |
---|---|
Create | POST |
Read | GET |
Update | PUT 或 PATCH |
Delete | DELETE |
比如不使用Restful风格时的一条请求是这样的/add?p1=1&p2=2
,使用后即可改写为/add/1/2
,引用一下来自how2j.cn的图来说明一下:
SpringMVC中使用Restful风格需在控制层的Java文件中进行配置
package org.westos.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SpringMVCTest3 {
@RequestMapping("/t5/{i}/{j}/{name}")
public String testRestful(@PathVariable int i, @PathVariable int j, @PathVariable String name, Model model){
String sum = i+j+" "+name;
model.addAttribute("message",sum);
return "hello";
}
}
运行项目后请求及结果为:
若请求中有与配置不匹配的类型,则报错,如:
method属性指定请求类型
用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等
例如,现在method方法指定为post方法,因为浏览器默认是get方法,所以会报错
@RequestMapping(value = "/t6/{i}/{j}/{name}",method = {RequestMethod.POST})
public String testRestful2(@PathVariable int i, @PathVariable int j, @PathVariable String name, Model model){
String sum = i+j+" "+name;
model.addAttribute("message",sum);
return "hello";
}