我正在使用Spring MVC創建一個小型Web應用程序。當我嘗試點擊一個似乎正確映射到我的控制器中的URL時,我得到一個404。我嘗試點擊/ projectname/test-,這是當我嘗試這樣做時在日誌中顯示的內容:Spring MVC - 在名爲'appServlet'的DispatcherServlet中找不到具有URI [/ demo/test]的HTTP請求的映射
警告:org.springframework.web.servlet.PageNotFound - 沒有在DispatcherServlet的發現HTTP請求的URI與[/演示/測試]名爲「appServlet」
我見過很多類似的問題被詢問的映射,但是沒有任何建議的修補程序適用於我。我使用的是Spring MVC,而服務器似乎無法解析通過瀏覽器進入的URL。奇怪的是「/ projectname」很好用,但「/ projectname/test」似乎根本不起作用。 JSP中都在同一個目錄中(src /主/ web應用/ WEB-INF /視圖/)具有下列名稱:
針對home.jsp,test.jsp的,userInfo.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"
\t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
\t <modelVersion>4.0.0</modelVersion>
\t <groupId>com.springapp</groupId>
\t <artifactId>demo</artifactId>
\t <name>demo</name>
\t <packaging>war</packaging>
\t <version>1.0.0-BUILD-SNAPSHOT</version>
\t <properties>
\t \t <java-version>1.6</java-version>
\t \t <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
\t \t <org.aspectj-version>1.6.10</org.aspectj-version>
\t \t <org.slf4j-version>1.6.6</org.slf4j-version>
\t </properties>
\t <dependencies>
\t \t <!-- Spring -->
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-context</artifactId>
\t \t \t <version>${org.springframework-version}</version>
\t \t \t <exclusions>
\t \t \t \t <!-- Exclude Commons Logging in favor of SLF4j -->
\t \t \t \t <exclusion>
\t \t \t \t \t <groupId>commons-logging</groupId>
\t \t \t \t \t <artifactId>commons-logging</artifactId>
\t \t \t \t </exclusion>
\t \t \t </exclusions>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-webmvc</artifactId>
\t \t \t <version>${org.springframework-version}</version>
\t \t </dependency>
\t \t \t \t
\t \t <!-- AspectJ -->
\t \t <dependency>
\t \t \t <groupId>org.aspectj</groupId>
\t \t \t <artifactId>aspectjrt</artifactId>
\t \t \t <version>${org.aspectj-version}</version>
\t \t </dependency> \t
\t \t
\t \t <!-- Logging -->
\t \t <dependency>
\t \t \t <groupId>org.slf4j</groupId>
\t \t \t <artifactId>slf4j-api</artifactId>
\t \t \t <version>${org.slf4j-version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.slf4j</groupId>
\t \t \t <artifactId>jcl-over-slf4j</artifactId>
\t \t \t <version>${org.slf4j-version}</version>
\t \t \t <scope>runtime</scope>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.slf4j</groupId>
\t \t \t <artifactId>slf4j-log4j12</artifactId>
\t \t \t <version>${org.slf4j-version}</version>
\t \t \t <scope>runtime</scope>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>log4j</groupId>
\t \t \t <artifactId>log4j</artifactId>
\t \t \t <version>1.2.15</version>
\t \t \t <exclusions>
\t \t \t \t <exclusion>
\t \t \t \t \t <groupId>javax.mail</groupId>
\t \t \t \t \t <artifactId>mail</artifactId>
\t \t \t \t </exclusion>
\t \t \t \t <exclusion>
\t \t \t \t \t <groupId>javax.jms</groupId>
\t \t \t \t \t <artifactId>jms</artifactId>
\t \t \t \t </exclusion>
\t \t \t \t <exclusion>
\t \t \t \t \t <groupId>com.sun.jdmk</groupId>
\t \t \t \t \t <artifactId>jmxtools</artifactId>
\t \t \t \t </exclusion>
\t \t \t \t <exclusion>
\t \t \t \t \t <groupId>com.sun.jmx</groupId>
\t \t \t \t \t <artifactId>jmxri</artifactId>
\t \t \t \t </exclusion>
\t \t \t </exclusions>
\t \t \t <scope>runtime</scope>
\t \t </dependency>
\t \t <!-- @Inject -->
\t \t <dependency>
\t \t \t <groupId>javax.inject</groupId>
\t \t \t <artifactId>javax.inject</artifactId>
\t \t \t <version>1</version>
\t \t </dependency>
\t \t \t \t
\t \t <!-- Servlet -->
\t \t <dependency>
\t \t \t <groupId>javax.servlet</groupId>
\t \t \t <artifactId>servlet-api</artifactId>
\t \t \t <version>2.5</version>
\t \t \t <scope>provided</scope>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>javax.servlet.jsp</groupId>
\t \t \t <artifactId>jsp-api</artifactId>
\t \t \t <version>2.1</version>
\t \t \t <scope>provided</scope>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>javax.servlet</groupId>
\t \t \t <artifactId>jstl</artifactId>
\t \t \t <version>1.2</version>
\t \t </dependency>
\t
\t \t <!-- Test -->
\t \t <dependency>
\t \t \t <groupId>junit</groupId>
\t \t \t <artifactId>junit</artifactId>
\t \t \t <version>4.7</version>
\t \t \t <scope>test</scope>
\t \t </dependency>
\t </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\t xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
\t <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
\t <context-param>
\t \t <param-name>contextConfigLocation</param-name>
\t \t <param-value>/WEB-INF/spring/root-context.xml</param-value>
\t </context-param>
\t
\t <!-- Creates the Spring Container shared by all Servlets and Filters -->
\t <listener>
\t \t <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
\t </listener>
\t <!-- Processes application requests -->
\t <servlet>
\t \t <servlet-name>appServlet</servlet-name>
\t \t <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
\t \t <init-param>
\t \t \t <param-name>contextConfigLocation</param-name>
\t \t \t <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
\t \t </init-param>
\t \t <load-on-startup>1</load-on-startup>
\t </servlet>
\t \t
\t <servlet-mapping>
\t \t <servlet-name>appServlet</servlet-name>
\t \t <url-pattern>/</url-pattern>
\t </servlet-mapping>
</web-app>
servlet的context.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\t xmlns:beans="http://www.springframework.org/schema/beans"
\t xmlns:context="http://www.springframework.org/schema/context"
\t xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
\t \t http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
\t \t http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
\t <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
\t
\t <!-- Enables the Spring MVC @Controller programming model -->
\t <annotation-driven/>
\t <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
\t <resources mapping="/resources/**" location="/resources/" />
\t <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
\t <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
\t \t <beans:property name="prefix" value="/WEB-INF/views/" />
\t \t <beans:property name="suffix" value=".jsp" />
\t </beans:bean>
\t
\t <context:component-scan base-package="com.springapp.demo" />
\t
\t
\t
</beans:beans>
HomeController.java:
package com.springapp.demo;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Handles requests for the application home page.
*/
@RequestMapping(value = "/")
@Controller
public class HomeController {
\t
\t private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
\t
\t /**
\t * Simply selects the home view to render by returning its name.
\t */
\t @RequestMapping(value = "/", method = RequestMethod.GET)
\t public String home(Locale locale, Model model) {
\t \t logger.info("Welcome home! The client locale is {}.", locale);
\t \t
\t \t return "home";
\t }
\t
\t @RequestMapping(value = "/user", method = RequestMethod.POST)
\t public String userInfo(Model model, @RequestParam(value="userName")String userName, @RequestParam(value="gender")String gender) {
\t \t \t \t
\t \t model.addAttribute("userName", userName);
\t \t model.addAttribute("gender", gender);
\t \t
\t \t return "userInfo";
\t }
\t @RequestMapping(value = "/test", method = RequestMethod.GET)
\t public String test(Model model) {
\t \t
\t \t return "test";
\t }
\t
}
任何想法?我需要提供更多信息嗎?謝謝!
我的推薦。遠離XML-s。如果你想要一個小型的Web應用程序,請考慮Spring Boot。 – Vaelyr 2014-09-29 18:39:56