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
<?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: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
<?xml version="1.0" encoding="UTF-8"?>
<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>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>
4.使用到的jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${message}
</body>
</html>

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 方法
CreatePOST
ReadGET
UpdatePUT 或 PATCH
DeleteDELETE

比如不使用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";
    }

在这里插入图片描述
如果将method方法指定为get则运行成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值