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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>spring</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring3mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>spring3mvc</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> </web-app> spring3mvc-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:p="http://www.springframework.org/schema/p" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true"> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <mvc:default-servlet-handler/> <!--使Spring支持自动检测组件,如注解的Controller --> <context:component-scan base-package="com.xp.web" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> <!-- 上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="2147483648" /> <!-- 2G --> </bean> <!-- 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> </beans> UserLogin.java package com.xp.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class UserLogin { @RequestMapping("/login") public String login() { System.out.println("======="); return "login"; } }
访问:http://localhost:8089/user/login.do,没异常,也没报错,是不是还缺配置项?
DefaultAnnotationHandlerMapping配置需要加上order属性,不然让其他的handler先处理了
[code="java"]
[/code]
http://localhost:8089/user/login.do
是不是路径写错了 没加上下文?
http://localhost:8089/{上下文}/user/login.do
tomcat启动异常没?如果没,访问http://localhost:8089试试,如果还没错,访问http://localhost:8089/spring/user/login.do试试
访问:http://localhost:8089/user/login.do,没异常,也没报错
这个说明根本就没请求到你的web应用中来。
1楼正解,需要你加上上下文。webapps目录下面部署的是spring应用么?直接访问这个url:http://localhost:8089/spring/user/login.do
是不是访问地址不对啊?加上项目名称试试.
例如:http://localhost:8089/项目名称/user/login.do
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
default-lazy-init="true">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<mvc:default-servlet-handler/>
<mvc:resources mapping="/**" location="/" order="1"/>
<!--使Spring支持自动检测组件,如注解的Controller -->
<context:component-scan base-package="com.spring.web" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
<!-- 上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="2147483648" /> <!-- 2G -->
</bean>
<!-- 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
<property name="order" value="0" />
</bean>
[/code]
配置文件改成这样,我试过了,可以
通过页面把请求转发过去试试
兄弟,你访问的URL地址中,怎么连项目名都没有?你是直接部署在Tomcat的 webapps\ROOT 目录下吗?否则,你的访问都应该是要带项目名的
@RequestMapping("/login.do")试试
应该不是访问路径的问题,我这两天也碰到这个问题,我用的idea,直接新建的maven项目,怎么配置都出这个问题,然后新建springmvc项目,又能正常访问,不知道问题在那,很神奇
@RequestMapping("/login") 改成这个@RequestMapping("/login.do")试试
上下文未添加;
http://localhost:8089/spring/user/login.do
或者注解url 改下 @RequestMapping("/login.do")