我无法执行基本的spring mvc应用程序。我已经尝试了所有可能的解决方案,最后想要解决这个问题。我不想创建一个新工作区并再次执行。我想让这个工作。这是项目的GitHub url。我正在登陆正确的页面(index.jsp)。
我尝试了所有这些
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
@RequestMapping
并从中更改
(/welcome)
至(value = "/welcome", method = {RequestMethod.GET,RequestMethod.POST})
有时候我在控制台中看到no-mapping-found-for-http-request-with-uri-in-dispatcherservlet-with-name
错误,有时却没有。
我知道有很多类似的问题,我已经经历了很多问题并开始了这个新线程。我的问题是
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>BasicSpringMVC1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>basicspringmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>basicspringmvc</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>/test.jsp</url-pattern>
<url-pattern>/test.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
basicspringmvc-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.basicSpringMvc1.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
的pom.xml
<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>BasicSpringMVC1</groupId>
<artifactId>BasicSpringMVC1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
SpringHelloWorld.java
package com.basicSpringMvc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpringHelloWorld {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from SpringHelloWorld.java **********</div><br><br>";
return new ModelAndView("welcome", "message", message);
}
}
项目结构
答案 0 :(得分:1)
请注意,在servlet映射中:
<servlet-mapping>
<url-pattern>/welcome.html</url-pattern>
</servlet-mapping>
您已为welcome.html
添加了网址格式。
但是您的控制器的请求映射设置为@RequestMapping("/welcome")
。
添加/welcome
的网址映射:
<servlet-mapping>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
以便可以映射到您的控制器。
或者将控制器的请求映射更改为@RequestMapping("/welcome.html")
,以便控制器映射到welcome.html
。
答案 1 :(得分:0)
检查这是否有帮助!将Web.xml中的servlet-mapping
条目更改为以下内容。你必须避免在你的web.xml中放入很多url-pattern
<servlet-mapping>
<servlet-name>basicspringmvc</servlet-name>
<url-pattern>*htm</url-pattern>
</servlet-mapping>