简体   繁体   中英

The requested resource is not available error in spring mvc project

I got an error as follows :

Status report

message: /SBC/loginform.html

description :The requested resource is not available.

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"
    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 is there at webContent. loginform.jsp is at webContent->web-Inf ->Views

Your dispatcherServlet is mapping urls like: /forms/* to the spring servlet.

You are requesting a url like: /SBC/loginform.html

This request will never go to the spring servlet. You have to access: /{your app context}/forms/loginform.html in order to go to the spring url mapped. Your app context seems to be "SBC", so it will be something like: /SBC/forms/loginform.html

Another way is change the Servlet mapping to:

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

This way, every request on your app context /SBC/* will be routed to the spring servlet. With this configuration, you should be able to access your controller with: /SBC/loginform.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM