JavaEE(14)SpringMVC中结果跳转方式、数据处理、乱码问题

1. 结果跳转

1. SpringMVC实现转发和重定向(不需要视图解析器)
(1)配置web.xml


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

(2)配置springmvc-servlet.xml


<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 http://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.nelws.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    
        
        
    
beans>

(3)编写Controller

@Controller
public class ControllerTest {
     

    @RequestMapping("/ct/t01")
    public String test01(Model model){
     
        model.addAttribute("msg","ControllerTest");
        //转发
        return "/WEB-INF/jsp/test.jsp";
    }

    @RequestMapping("/ct/t02")
    public String test02(){
     
        //转发
        return "forward:/index.jsp";
    }

    @RequestMapping("/ct/t03")
    public String test03(){
     
        //重定向
        return "redirect:/index.jsp";
    }
}

(4)编写视图界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
${msg}
body>
html>

(5)测试

2. SpringMVC实现转发和重定向(需要视图解析器)
(1)配置web.xml


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

(2)配置springmvc-servlet.xml


<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 http://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.nelws.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)编写Controller

@Controller
public class ControllerTest2 {
     

    @RequestMapping("/ct2/t01")
    public String test01(Model model){
     
        model.addAttribute("msg","ControllerTest2");
        //转发
        return "test";
    }

    @RequestMapping("/ct2/t02")
    public String test02(){
     
        //转发
        return "forward:/index.jsp";
    }

    @RequestMapping("/ct2/t03")
    public String test03(){
     
        //重定向
        return "redirect:/index.jsp";
    }
}

(4)编写视图界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
${msg}
body>
html>

(5)测试

2. 数据处理

1. 提交数据处理
(1)提交的域名城和处理方法的参数名称一致:localhost:8080/dc/t01?name=laowang

    @RequestMapping("/dc/t01")
    public String test01(String name){
     
        System.out.println(name);  //laowang
        return "test";
    }

(2)提交的域名城和处理方法的参数名称不一致:localhost:8080/dc/t02?username=xiaoli

    @RequestMapping("/dc/t02")
    public String test02(@RequestParam("username") String name){
     
        System.out.println(name);  //xiaoli
        return "test";
    }

(3)提交的是一个对象:localhost:8080/dc/t03?id=1&name=haha&age=13

使用对象的话,前端传递的参数名和对象名必须一致,否则就是null

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
     
    private int id;
    private String name;
    private int age;
}
    @RequestMapping("/dc/t03")
    public String test03(User user){
     
        System.out.println(user);  //User(id=1, name=haha, age=13)
        return "test";
    }

2. 显示数据到前端
(1)通过ModelAndView(和以前一样 不做过多解释)

public class ControllerTest1 implements Controller {
     
 
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
     
        //返回一个模型视图对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","ControllerTest1");
        mv.setViewName("test");
        return mv;
    }
}

(2)通过ModelMap:http://localhost:8080/sc/t01?name=laowang

    @RequestMapping("/sc/t01")
    public String test01(String name, ModelMap map){
     
        map.addAttribute("msg",name);
        System.out.println(name);  //laowang
        return "test";
    }

(3)通过Model:http://localhost:8080/sc/t02?name=xiaoli

    @RequestMapping("/sc/t02")
    public String test01(String name, Model model){
     
        model.addAttribute("msg",name);
        System.out.println(name);  //xiaoli
        return "test";
    }

(4)对比

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

3. 中文乱码问题

1. 解决办法:使用SpringMVC自带的过滤器,在web.xml中配置

    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

你可能感兴趣的:(java)