2016-07-18 14 views
0

ich bin neu bei Spring MVC.wenn ich das Projekt starte, wird welcome.jsp geöffnet. dann fülle ich das Formular aus und drücke Login. Es ist alles wie es sollte bis jetzt aber dann möchte ich die success.jsp öffnen, wenn die userid ist didem und die passqord ist 123321 wenn nicht dann welcome.jsp sollte wieder öffnen .. aber was ich tatsächlich bekomme 404 ist nicht gefunden weil in der Web-Browser wie dies die uRL Link lautet: http://localhost:8080/CrunchifySpringMVCTutorial/welcome Idk, warum die viewResolver eine .jsp am Ende gesetzt tut .. Jede Hilfe zu schätzen ..Spring MVC .jsp wird nicht automatisch von ViewResolver erstellt

CrunchifyHelloWorld.java Datei würde:

 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-Datei:

<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-Datei:

<?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 Datei:

<%@ 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 Datei:

<%@ 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

Konsolenprotokolle:

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 
+0

Dies ist nicht in Bezug auf .jsp Suffixed aber etwas, das nicht am richtigen Ort ist! –

+0

Bitte fügen Sie in der erweiterten Projektverzeichnisstruktur des Projekts –

+0

Theres ein Teil "Bildbeschreibung hier eingeben" Ich fügte den Pic Link, aber konnte den DESC nicht entschuldigen :) – Didem

Antwort

0

Ihre Servletzuordnung zu folgenden Zeilen ändern und

<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> 
+0

zumindest nicht mehr 404 Fehler, aber es öffnet die welcome.jsp ich didem für die Benutzername und 123321 für das Passwort, aber immer noch das "welcome.jsp" öffnen – Didem

+0

Entschuldigung für die späte Antwort. Ich denke, Sie haben das Problem vielleicht gelöst, aber ich möchte trotzdem meine Meinung teilen. Ich denke, das Problem besteht im String-Vergleich. Verwenden Sie die .equals-Methode der String-Klasse. – BeginnersSake

+0

if (userID.equals ("didem") && passwort.equals ("123321")) { zurück "Erfolg"; } – BeginnersSake

0

Chaneg unten Tag in web.xml und versuchen einmal überprüfen.

<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> 
0

was ist mit Rückkehr "redirect:/{welcome}"; Wie nachfolgend dargestellt.

@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}"; 



    } 
} 
+0

Ich habe gerade herausgefunden, wo das Problem liegt. wenn ändern wenn zu (1 == 1) {zurück "Erfolg"; } Rückkehr "Willkommen"; dann öffnet es die Erfolgsseite .. so erhält die Anforderung param Sache nicht die Benutzereingabe: D – Didem

+0

gut, aber eine gute Praxis ist, ein Frühlingssicherheitsmuster zu implementieren. –

+1

sicher, ich beginne es einfach – Didem