parsing XML document from class path resource [WEB-INF/spring-servlet.xml]
springMVC做项目时,发生的错误。
下面是servlet.xml
<!-- 使用annotation注解的方式完成映射 --> <context:component-scan base-package="cn"/> <mvc:annotation-driven/> <!-- 视图解析 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>
下面是web.xml
<servlet> <servlet-name>welcome</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
把servlet.xml放在resource下时
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:resource/spring-servlet.xml</param-value> </init-param>
这样配启动tomcat没报错,但是请求时报:
警告: No mapping found for HTTP request with URI [/SpringMvc01/hello] in DispatcherServlet with name 'welcome'
以下是Controller类的代码:
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String hello(String username , Model model){ model.addAttribute("username" , username); return "welcome"; }
请求的URL是 http://localhost:8080/SpringMvc01/hello?username=a
@RequestMapping("/hello")
@Controller
public class HelloController {
请求的URL是 http://localhost:8080/SpringMvc01/hello/hello?username=a
谢谢回答,问题已经解决了。在web.xml配置文件中
servlet-name必须和 *-servlet.xml中的 * 一致。