我的IDE是VSCODE
tomcat服务器连接正常但是控制器不工作输入"http://localhost:8080/sp5-chap09/hello?name=abc"出现404错误;控制器的println不起作用,因此控制台看不到log
当我访问"http://localhost:8080/sp5-chap09/src/main/webapp/WEB-INF/view/hello.jsp"时,jsp文件显示在浏览器中,没有404错误发生,但是不能导入参数
工作区树:
SP5-CHAP09
| pom.xml
|
+---src
| ---main
| +---java
| | +---chap09
| | | HelloController.java
| | |
| | ---config
| | ControllerConfig.java
| | MvcConfig.java
| |
| +---resources
| ---webapp
| ---WEB-INF
| | web.xml
| |
| ---view
| hello.jsp
|
---target
+---classes
| +---chap09
| | HelloController.class
| |
| ---config
| ControllerConfig.class
| MvcConfig.class
|
+---generated-sources
| ---annotations
+---maven-archiver
| pom.properties
|
+---maven-status
| ---maven-compiler-plugin
| ---compile
| ---default-compile
| createdFiles.lst
| inputFiles.lst
|
---test-classes
控制器:
package chap09;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model,
@RequestParam(value="name", required=false) String name){
model.addAttribute("greeting", "Hello, " + name);
System.out.println("hello() method execute: " + LocalDateTime.now());
return "hello";
}
}
配置文件:
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import chap09.HelloController;
@Configuration
public class ControllerConfig {
@Bean
public HelloController helloController(){
return new HelloController();
}
}
package config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer{
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry){
registry.jsp("/WEB-INF/view/", ".jsp");
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sp5</groupId>
<artifactId>sp5-chap09</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b02</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
web . xml:
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
config.MvcConfig
config.ControllerConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
视图文件:
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
greeting: ${greeting}
</body>
</html>
我搜索了,但是没有适合我的解决方案我猜不出来
我解决了。
- 当你在maven上工作时,maven项目任务行在vscode的左下方创建。
- 如果单击任务行,则工作空间中的maven项目将以列表形式出现。
- 其中,右键单击要操作的目录,在出现的列表中选择"package"。
- maven-war-plugin版本错误。
- 在pom.xml中添加了maven-war-plugin 3.2.2。
- 如果你再次选择'package',在目标文件夹中创建一个war文件。
- 添加在tomcat服务器中创建的war文件。 如果你在浏览器中显示,maven项目可以正常工作。
- 你必须点击'package'每次编辑代码。