HTTP状态404 - 在Spring 3.2.7中找不到

时间:2015-06-05 05:08:51

标签: spring

我使用的是Spring 3.2.7,我有一个接受输入的Login.jsp文件,应该在hello.jsp上显示输出,它接受输入但不显示输出,而是抛出错误{{1 }}。这是代码:

HTTP Status 404 - Not Found

分派器-servlet.xml中

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        if (password.equals("admin")) {
            String message = "Hello " + name;
            return new ModelAndView("hellopage", "message", message);
        } else {
            return new ModelAndView("errorpage", "message", "Sorry Username or Password Error");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在观察Dispatcher-servlet.xml之后,我发现你遗失了<mvc:annotation-driven/> 您更新的代码如下所示

    <?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-3.0.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd"
                   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">                                            

        <mvc:annotation-driven/>  
        <context:component-scan base-package="mypack" />
        <bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp" />
        </beans>

<强>解释

  

<mvc:annotation-driven/>表示您可以定义spring bean   依赖,而不必实际指定一堆元素   xml或实现接口或扩展基类.Means @Controller   告诉spring,指定的类包含将要的方法   处理http请求而无需实现Controller   接口或扩展实现控制器的子类。

     

<context:component-scan base-package="mypack />告诉春天   应该在mypacktwo reversals下搜索所有类的类路径   查看每个类以查看它是否具有@Controller或@Repository,或者   @Service,或@Component,若是,那么Spring会注册   bean工厂的类,就像你在xml配置文件中定义的一样

答案 1 :(得分:0)

  

不,我在Dispatcher-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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   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.1.xsd
   http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">

<context:component-scan base-package="mypack" />
<mvc:annotation-driven />

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />
</beans>

still am getting an error as "Status 404 - Not Found" when i submit the form.

Form:

<form action="hello.htm" method="post">
        Name: <input type="text" name="name" /><br>
        Password: <input type="password" name="password" />
        <input type="submit" value="Login" />
    </form>

这是Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>