SpringMvc基础配置+转发和重定向

一、配置DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<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>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>

二、配置视图解析器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: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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--作用域,在此作用域下注解生效-->
    <context:component-scan base-package="com.liu.controller"/>
    <!--mvc不处理静态资源-->
    <mvc:default-servlet-handler/>
    <!--支持mvc注解驱动-->
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

三、注解配置、重定向和转发、不使用视图注解器

重定向不走视图解析器

package com.liu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller//相当于<bean id="" class=""/>
    public class ViewControllerTest {
    
    
    /*使用视图解析*/
    @RequestMapping("Test")
    public String testOne(String name,Model model){
    
    
        System.out.println(name);
        model.addAttribute("msg","没有视图解析器"+name);
        //转发相当于Request.getRequestDispatcher("路径").forward(req,rsq)
        return "hello";
    }
   /*这下面的是注释了spring_mvc中的视图解析器*/
    //不要视图解析器转发:http://localhost:8080/notView
    @RequestMapping("/notView") //相当于在web中配置servlet
    public String NotViewMappping(int a,Model model){
    
    
        //a对应前端传过来的值
        System.out.println(a);
        model.addAttribute("msg","没有视图解析器"+a);
        //转发相当于Request.getRequestDispatcher("路径").forward(req,rsq)
        return "/WEB-INF/jsp/hello.jsp";
    }
    //不要视图解析器转发:http://localhost:8080/showForward
    @RequestMapping("/showForward")
    public String ShowForward(Model model){
    
    
        model.addAttribute("msg","没有视图解析器,显示forward");
        return "forward:/WEB-INF/jsp/hello.jsp";
    }
    //重定向
    @RequestMapping("/change")
    public String ShowRedirect(Model model){
    
    
        model.addAttribute("msg","redirect");
        return "redirect:/index.jsp";
    }
    //有视图解析
    @RequestMapping("/have")
    public String ViewRedirect(Model model){
    
    
        model.addAttribute("msg","redirect");
        return "redirect:/index.jsp";
    }

}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44774287/article/details/124107337
今日推荐