ViewResolver不会自动创建Spring MVC .jsp

时间:2016-07-18 11:31:24

标签: java jsp spring-mvc servlets

我是Spring MVC的新手。当我运行项目welcome.jsp打开时。然后我填写表格并按登录。直到现在它应该是全部,但是如果userID是didem并且passqord是123321,那么我想要success.jsp打开然后welcome.jsp应该重新打开..但实际得到的是404未找到 因为在网络浏览器中,网址链接如下所示:http://localhost:8080/CrunchifySpringMVCTutorial/welcome Idk为什么viewResolver没有把.jsp放在最后..任何帮助将不胜感激..

CrunchifyHelloWorld.java文件:

     package com.crunchify.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;

@Controller
public class CrunchifyHelloWorld {


    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String showLoginForm() {

        return "welcome";
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.POST)
    public String verifyLogin(@RequestParam String userID,  @RequestParam String password){

        ModelAndView model = new ModelAndView();
        model.addObject("loginError", "Invalid Id AND/OR password");
        if(userID == "didem" && password == "123321")
        {
            return "success";
        }
        return "redirect:/";



    }
}

crunchify-servlet.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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">

    <context:component-scan base-package="com.crunchify.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>CrunchifySpringMVCTutorial</display-name>
  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>crunchify</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>crunchify</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

welcome.jsp文件:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>

<!-- let's add tag spring:url -->
<spring:url value="/resources/crunchify.css" var="crunchifyCSS" />
<spring:url value="/resources/crunchify.js" var="crunchifyJS" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link href="${crunchifyCSS}" rel="stylesheet" />
<script src="${crunchifyJS}"></script>
<!-- Finish adding tags -->

<title>Spring MVC Tutorial by Crunchify - Hello World Spring MVC Example</title>
<style type="text/css">
body {
    background-image: url('http://cs-im.psn-web.net/Global/SIPPHONE/sipphone_net/download/UT670/wallpaper/gray_phone_background_plane.png');
}
</style>
</head>
<body>

<!-- webapp content goes here in the body -->

<div class = "container">
${loginError}
<div class = "form-group form" >
   <form action = 'welcome' method = "POST">
      <div>
         <label>User name:</label>
         <input type = "text" id = "userID" name = "userID" placeholder = "your username" class = "form-control">
      </div>
      <div>
         <label>Password:</label>
         <input type = "password" id = "password" name = "password" placeholder = "your password" class = "form-control">
      </div>
      <button id = "loginButton" class = "form-control">LOGIN</button>
   </form>
</div>
</div>
</body>
</html>

success.jsp文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
     CONGRATS. YOU SUCCESSFULLY LOGGED IN ! 
</body>
</html>

HERE IS THE EXPANDED STRUCTURE OF MY PROJECT

CONSOLE LOGS:

Jul 18, 2016 3:06:31 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CrunchifySpringMVCTutorial' did not find a matching property.
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.36
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jun 9 2016 13:55:50 UTC
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.36.0
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\dev\Java\jdk8\jre
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_74-b02
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\User\Desktop\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files (x86)\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\User\Desktop\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files (x86)\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\User\Desktop\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files (x86)\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\endorsed
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jul 18, 2016 3:06:31 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\dev\Java\jdk8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/dev/Java/jre8/bin/server;C:/dev/Java/jre8/bin;C:/dev/Java/jre8/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\dev\Java\jdk8\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\gradle-2.2.1\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Git\bin;C:\Program Files (x86)\Git\cmd;C:\Users\User\AppData\Roaming\npm;C:\Users\User\eclipse\java-mars\eclipse;;.
Jul 18, 2016 3:06:31 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jul 18, 2016 3:06:31 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jul 18, 2016 3:06:31 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jul 18, 2016 3:06:31 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jul 18, 2016 3:06:31 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 493 ms
Jul 18, 2016 3:06:31 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 18, 2016 3:06:31 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.36
Jul 18, 2016 3:06:32 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 18, 2016 3:06:32 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jul 18, 2016 3:06:32 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'crunchify'
Jul 18, 2016 3:06:32 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'crunchify': initialization started
Jul 18, 2016 3:06:32 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'crunchify-servlet': startup date [Mon Jul 18 15:06:32 EEST 2016]; root of context hierarchy
Jul 18, 2016 3:06:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/crunchify-servlet.xml]
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/welcome],methods=[POST]}" onto public java.lang.String com.crunchify.controller.CrunchifyHelloWorld.verifyLogin(java.lang.String,java.lang.String)
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/welcome],methods=[GET]}" onto public java.lang.String com.crunchify.controller.CrunchifyHelloWorld.showLoginForm()
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'crunchify-servlet': startup date [Mon Jul 18 15:06:32 EEST 2016]; root of context hierarchy
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'crunchify-servlet': startup date [Mon Jul 18 15:06:32 EEST 2016]; root of context hierarchy
Jul 18, 2016 3:06:33 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'crunchify': initialization completed in 875 ms
Jul 18, 2016 3:06:33 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jul 18, 2016 3:06:33 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jul 18, 2016 3:06:33 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1977 ms

3 个答案:

答案 0 :(得分:0)

将servlet映射更改为以下行并检查

 <servlet-mapping>
        <servlet-name>crunchify</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

答案 1 :(得分:0)

在web.xml中标记下面的chaneg并尝试一次。

<servlet>
        <servlet-name>crunchify</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <init-param> 
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/crunchify-servlet.xml </param-value> 
    </init-param>   
        <load-on-startup>1</load-on-startup>
    </servlet>

答案 2 :(得分:0)

返回“redirect:/ {welcome}”怎么样;如下所示。

@RequestMapping(value = "/welcome", method = RequestMethod.POST)
    public String verifyLogin(@RequestParam String userID,  @RequestParam String password){

        ModelAndView model = new ModelAndView();
        model.addObject("loginError", "Invalid Id AND/OR password");
        if(userID == "didem" && password == "123321")
        {
            return "success";
        }
        return "redirect:/{welcome}";



    }
}