SpringMVC--数据处理及跳转

1、结果跳转方式

方式一、ModelAndView

设置ModelAndView对象,根据View名称,和视图解析器跳转到指定页面

  • 创建类,实现Controller接口
public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //ModelAndView  模型和视图
        ModelAndView mv = new ModelAndView();

        //封装对象
        mv.addObject("msg","HelloServlet");
        //封装要跳转的视图
        mv.setViewName("test"); // /WEB-INF/jsp/test.jsp
        return mv;
    }
}
  • 在spring配置文件中编写视图解析器
<!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
方式二、ServletAPI

使用ServletAPI,就不需要视图解析器了
可以实现输出,重定向,转发

@Controller
public class Controller05 {
    //输出
    @RequestMapping("/servlet/t1")
    public void test1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().println("hello,servlet!");
    }

    //重定向
    @RequestMapping("/servlet/t2")
    public void test2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("/index.jsp");
    }

    //转发
    @RequestMapping("/servlet/t3")
    public void test3(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("msg","t3");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
    }
}
方式三、SpringMVC

通过springMVC来实现转发和重定向

  • 不需要视图解析器的方式
@Controller
public class Controller06 {
    @RequestMapping("/mvc/t1")
    public String hello(){
        return "index.jsp";//转发
    }
    @RequestMapping("/mvc/t2")
    public String hello2(){
        //forward 转发
        return "forward:/WEB-INF/jsp/hahaha.jsp";
    }
    @RequestMapping("/mvc/t3")
    public String hello3(){
        //重定向
        return "redirect:/index.jsp";
    }
}
  • 需要视图解析器的方式
@Controller
public class Controller07 {
    @RequestMapping("mvc2/t1")
    public String hello(){
        return "hahaha";//转发
    }
    @RequestMapping("mvc2/t2")
    public String hello2(){
        return "redirect:/index.jsp";//重定向   重定向不需要视图解析器,本质就是重新请求一个新地方,注意路径问题
    }
}

2、数据处理

2.1处理提交数据
  1. 提交的域名和处理方法的参数名一致时

提交的域名:http://localhost:8080/ljy/t1?name=lijinyang

@Controller
@RequestMapping("/ljy")
public class Controller08 {
    @RequestMapping("/t1")
    public String test(String name, Model model){
        model.addAttribute("msg",name);//前端显示
        System.out.println(name);//控制台输出
    return "test";
    }
}
  1. 提交的域名和处理方法的参数名不一致时

提交的数据:http://localhost:8080/ljy/t1?username=lijinyang

@Controller
@RequestMapping("/ljy")
public class Controller08 {
    @RequestMapping("/t1")
    public String test(@RequestParam("username") String name, Model model){
        model.addAttribute("msg",name);
        System.out.println(name);
    return "test";
    }
}
  1. 提交的数据是一个对象时

提交数据:http://localhost:8080/ljy/t1?id=1&age=18&name1=lijinyang

@Controller
@RequestMapping("/ljy")
public class Controller08 {
    @RequestMapping("/t1")
    public String test(User user, Model model){
        model.addAttribute("msg",user);
        System.out.println(user);
    return "test";
    }
}

注意:前端传递的参数名和对象名必须一致,否则就是null。但是顺序可以打乱

2.2、数据显示到前端

第一种、通过ModelAndView
第二种、通过ModelMap
第三种、通过Model

三种方式的简单对比:

  • Model:只有简单的几个储存方法,简化了新手的操作
  • ModelMap:继承了LinkedMap,除了实现自身的一些方法,也继承了LinkedMap的方法和特性
  • ModelAndView:可以在存储数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转

3、乱码问题

1、编写一个表单网页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/t1" method="post">
    <input type="text" name="name">
    <input type="submit">
</form>
</body>
</html>

2、编写对应的类

@Controller
public class Controller09 {
    @RequestMapping("/t1")
    public String test(Model model,String name){
        model.addAttribute("msg",name);
        return "test";
    }
}

3、输入中文测试,发现出现乱码
在这里插入图片描述

解决方法

利用SpringMVC提供的过滤器
在web.xml文件中增加下面这段代码

<!--springmvc过滤器-->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值