今天完成了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"; } }
标签:逻辑,SpringMVC,springframework,jsp,视图,org,import,annotation From: https://www.cnblogs.com/rsy-bxf150/p/17798710.html