Spring Mvc Controller部分未加载。

时间:2017-10-12 10:46:07

标签: java spring spring-mvc

当我点击<a href="/login">test</a>链接或<input type="submit" value="Submit"/>控制器部分未加载时。 Println语句未执行

<form action="/login" method="get">
<h2>UserName</h2><input type="text" name="userName"/>
<input type="submit" value="Submit"/>
</form>
<a href="/login">test</a>

的web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet> 
 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

弹簧servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="com.sample.*" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

控制器部分:

@Controller

public class UserController
{   

    @RequestMapping("/login") 
    public void loginUser() throws Exception  
    {
        System.out.println("Inside loginUser method");

    }
}

1 个答案:

答案 0 :(得分:1)

我发布解决方案,如果有人遇到同样的问题:

更改action="/login" to action="login"<a ref="/login">test</a> to <a href="<c:url value="/login" />"></a>