SpringMVC结果跳转方式、Restful风格、表单数据处理

SpringMVC结果跳转方式、Restful风格

一、结果跳转方式一——ModelAndView

1.pojo实体类

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 +
                '}';
    }
}

2.springmvc-servlet.xml


<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>

3.web.xml


<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>

4.使用到的jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


${message}



5.ModelAndView方式实现跳转

返回的是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;
    }
}

二、结果跳转方式二——ServletAPI

只有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);
}

三、结果跳转方式三——SpringMVC不使用视图解析器

使用forward转发方式,redirect重定向方式不能携带参数,只能实现无参数跳转

//SpringMVC不带视图解析器
@RequestMapping("t3")
public String test3(HttpServletRequest request){
    request.setAttribute("message","test3");
    return "forward:WEB-INF/jsp/hello.jsp";
}

四、结果跳转方式四——SpringMVC带视图解析器(最常用)

//SpringMVC带试图带视图解析器
@RequestMapping("t4")
public String test4(Model model){
    model.addAttribute("message","test4");
    return "hello";
}

五、Restful风格

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风格、表单数据处理_第1张图片
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";
    }
}

运行项目后请求及结果为:
SpringMVC结果跳转方式、Restful风格、表单数据处理_第2张图片
若请求中有与配置不匹配的类型,则报错,如:
SpringMVC结果跳转方式、Restful风格、表单数据处理_第3张图片
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";
    }

SpringMVC结果跳转方式、Restful风格、表单数据处理_第4张图片
如果将method方法指定为get则运行成功

你可能感兴趣的:(框架)