我是springmvc的初学者,我用maven在eclipse中创建了一个webapp项目,并用java纯代码进行了配置。在项目中没有错误,但当我运行它时,它不工作,它给我HTTP404错误[/springcalculator/test]不可用。springcalculator是我的名字,project test是controller urlMap
类calculatorapplicationinitializer,用于替换xml配置中的web.xml
public class CalculatorApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext WebApplicationContext = new
AnnotationConfigWebApplicationContext();
WebApplicationContext.register(CalculatorAppContext.class);
// create servlet Dispacher object
DispatcherServlet dispatcherServlet = new
DispatcherServlet(WebApplicationContext);
//registred dispatcher servlet with servlet context
ServletRegistration.Dynamic myCustomDispatcherServlet =
servletContext.addServlet("MyDispatcherServlet",dispatcherServlet);
myCustomDispatcherServlet.setLoadOnStartup(1);
myCustomDispatcherServlet.addMapping("/");
}
}
在xml配置中替换applicationcontext.xml的类calculatorappcontext
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.home.lc.controllers")
public class CalculatorAppContext {
// setup my view resolver
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
That's the controller returning vue
@Controller
public class TestController {
@RequestMapping("/test")
public String SayHello() {
return "hi" ;
}
}
vue高.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>:)</title>
</head>
<body>
<p> Hello </p>
</body>
</html>
我刚刚在pom.xml中添加了对spring-webmvc的依赖
<?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>com.home</groupId>
<artifactId>spring-calculator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-calculator Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
</dependencies>
<build>
<finalName>spring-calculator</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may
be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-
bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我的项目有一个结构
暂无答案!
目前还没有任何答案,快来回答吧!