响应视图

This commit is contained in:
2026-10-29 09:54:51 +08:00
parent bd074eb465
commit 4320a98eef
7 changed files with 89 additions and 3 deletions
+12
View File
@@ -32,4 +32,16 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>${basedir}/web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -1,6 +1,7 @@
package com.ycjy;
import com.ycjy.confiig.AppConfig;
import com.ycjy.confiig.WebConfig;
import org.jspecify.annotations.Nullable;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@@ -12,7 +13,7 @@ public class MvcInit extends AbstractAnnotationConfigDispatcherServletInitialize
@Override
protected Class<?> @Nullable [] getServletConfigClasses() {
return new Class[]{AppConfig.class};
return new Class[]{AppConfig.class, WebConfig.class};
}
@Override
@@ -2,10 +2,8 @@ package com.ycjy.confiig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan({"com.ycjy.web", "com.ycjy.exception"})
public class AppConfig {
}
@@ -0,0 +1,22 @@
package com.ycjy.confiig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.ycjy.web")
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
@@ -0,0 +1,38 @@
package com.ycjy.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
public class RequestView {
@RequestMapping("/v1")
public ModelAndView mv(){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
mv.addObject("name", "张三");
return mv;
}
@RequestMapping("/v2")
public String m(Model model){
model.addAttribute("name", "zs");
return "index";
}
@RequestMapping("/v3")
public String mm(ModelMap modelMap) {
modelMap.addAttribute("name", "张三");
return "index";
}
@RequestMapping("/v4")
public String map(Map<String, Object> map) {
map.put("name", "张三");
return "index";
}
}
@@ -0,0 +1,9 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%= request.getAttribute("name") %>
</body>
</html>
+6
View File
@@ -0,0 +1,6 @@
<?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">
</web-app>