spring mvc project中请求的资源不可用错误

时间:2015-06-03 12:52:03

标签: spring spring-mvc

我收到如下错误:

状态报告

消息:/SBC/loginform.html

description:请求的资源不可用。

调度-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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/Views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>



</beans>

的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>OpticareVisionHouse</display-name>


    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/forms/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>



    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/resources/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
     <context-param>

    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </context-param>

</web-app>

-------------------------------------------------
LoginController.java

@Controller
@RequestMapping("loginform.html")
public class LoginController {
    @Autowired
    public LoginService loginService;

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        LoginForm loginForm = new LoginForm();
        model.put("loginForm", loginForm);
        System.out.print("controller calls1");
        return "loginform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid LoginForm loginForm, BindingResult result,
            Map model) {
        System.out.print("controller calls2");

        if (result.hasErrors()) {
            return "loginform";
        }

        /*
        String userName = "UserName";
        String password = "password";
        loginForm = (LoginForm) model.get("loginForm");
        if (!loginForm.getUserName().equals(userName)
                || !loginForm.getPassword().equals(password)) {
            return "loginform";
        }
        */
        boolean userExists = loginService.checkLogin(loginForm.getUserName(),
                loginForm.getPassword());
        if(userExists){
            model.put("loginForm", loginForm);
            return "loginsuccess";
        }else{
            result.rejectValue("userName","invaliduser");
            return "loginform";
        }

    }
------------------------------------------------
index.jsp

<% response.sendRedirect("loginform.html"); %>

------------------------------------------------

index.jsp在webContent。 loginform.jsp位于webContent-&gt; web-Inf - &gt; Views

1 个答案:

答案 0 :(得分:0)

您的dispatcherServlet将URL:/forms/*映射到spring servlet。

您正在申请以下网址:/SBC/loginform.html

此请求永远不会转到spring servlet。您必须访问:/{your app context}/forms/loginform.html才能转到映射的spring url。您的应用上下文似乎是“SBC”,因此它类似于:/SBC/forms/loginform.html

另一种方法是将Servlet映射更改为:

 <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

这样,应用程序上下文/SBC/*上的每个请求都将路由到spring servlet。 使用此配置,您应该能够使用以下命令访问控制器:/SBC/loginform.html