公司使用的框架是jfinal,開發(fā)工具用的NetBeans,最近開始倒騰sPRingMVC了,就寫了下Helloworld,中間還遇到了一個坑嗲的問題,那就是No mapping found for HTTP request with URI [/springmvc-01/] in DispatcherServlet with name 'dispatcherServlet'。
在網(wǎng)上找了很多資料,web.xml中配置的錯誤,<url-pattern>/*</url-pattern>改為/,把*去掉,但是很明顯,我用的就是/。
后來挨個看配置文件也沒有問題,項目啟動的時候就是訪問不到index.jsp。
仔細看了下,我的index.jsp放到了WEB-INF下面了,所有才訪問不到的,index.jsp是和WEB-INF同級的。估計這個超低級的錯誤,只有我犯過吧。。
--------------------------------------------------------------------------------------------------------------------------------------------------
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_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置 DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcherServlet-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>dispatcherServlet-servlet.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定掃描的包 --> <context:component-scan base-package="com.kevin.controller"></context:component-scan> <!-- 配置視圖解析器: 如何把 handler 方法返回值解析為實際的物理視圖 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 在實際開發(fā)中通常都需配置 mvc:annotation-driven 標簽 --> <mvc:annotation-driven></mvc:annotation-driven></beans>HelloWorld.classpackage com.kevin.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld { /** * 使用@RequestMapping來映射URL * 返回值會被視圖解析器解析成物理試圖,試圖解析器會做如下解析 * prefix+return值+后綴,得到實際視圖,然后做轉(zhuǎn)發(fā)操作 * @return */ @RequestMapping("/helloworld") public String hello(){ System.out.println("HelloWorld"); return "success"; }}
新聞熱點
疑難解答