web mvc项目,xml读取的到控制器,下面是mvc配置
<!-- 启动注解驱动的spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<context:component-scan base-package="co.controller"/>
<mvc:default-servlet-handler />
web.xml配置
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
控制器
@Controller
@RequestMapping(value = "/")
public class mainController {
@Autowired
private TestService testService;
@RequestMapping(value = "login", method = RequestMethod.GET)
public String login(
){
System.out.print("login\n");
//view/index.jsp
return "login";
}
tomcat app context 为默认的
运行结果
警告 [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'spring'
一直找不到原因,原本的项目在eclipse里可以正常运行
spring核心控制器DispatcherServlet 你没有在web.xml配置吗?
<servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 读取类似struts框架的配置文件struts.xml,该配置文件在springmvc的默认命名规则为:servlet的名字+" -servlet.xml " 如想自己制定springmvc的配置文件,则应进行以下配置: --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext-mvc.xml</param-value> </init-param> <!-- 项目一启动就加载SpringMVC的分发器 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>