SpringMVC-快速返回逻辑视图/转发和重定向

今天完成了SpringMVC-快速返回逻辑视图以及转发和重定向,可以看作是springmvc一个非常基础的demo

效果:

 代码:

index.jsp

复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <font color="red">${data}</font>
</body>
</html>
复制代码

 

复制代码
package com.aurora.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan("com.aurora.jsp")
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        //可以快速添加前后缀
        registry.jsp("/WEB-INF/views/",".jsp");
    }
}
复制代码

 

复制代码
package com.aurora.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringMVCInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
复制代码

 

复制代码
package com.aurora.jsp;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("jsp")
public class JspController {
    /**
     * TODO:快速查找视图
     *   1.方法的返回值是字符串类型
     *   2.不能添加@ResponseBody,直接返回字符串给浏览器,不找视图,不走视图解析器
     *   3.返回值 对应中间的视图名称即可
     */
    @GetMapping("index")
    public String index(HttpServletRequest request){
        request.setAttribute("data","hello jsp!!hello Aurora!!");
        System.out.println("JspController.index");
        return "index";
    }

    /**
     * TODO:转发 只能是项目下的资源
     *    1.方法的返回值写成字符串
     *    2.不能添加@ResponseBody
     *    3.返回的字符串前加forward:/转发地址
     */
    @GetMapping("forward")
    public String forward(){
        System.out.println("JspController.forward");
        return "forward:/jsp/index";
    }
    /**
     * TODO:重定向
     *      1.方法的返回值写成字符串
     *      2.不能添加@ResponseBody
     *      3.返回的字符串前加redirect:/转发地址
     */
    @GetMapping("redirect")
    public String redirect(){
        System.out.println("JspController.redirect");
        return "redirect:/jsp/index";
    }
    @GetMapping("redirect/baidu")
    public String redirectBaidu(){
        System.out.println("JspController.redirectBaidu");
        return "redirect:http://www.baidu.com";
    }
}
复制代码

 

作者:冰稀饭Aurora

出处:https://www.cnblogs.com/rsy-bxf150/p/17798710.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   冰稀饭Aurora  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示