2016-08-18 44 views
0

我正面臨此錯誤在DispatcherServlet中找不到使用URI [/LoginApp/displayForm.html]的HTTP請求的映射名稱'java4s'在名爲'java4s'的DispatcherServlet中使用URI [/LoginApp/displayForm.html]發現HTTP請求沒有找到映射

此頁面重定向但現在不重定向。

我的代碼如下

Java4sController.java

package java4s; 

import javax.validation.Valid; 
import javax.servlet.http.HttpServletRequest; 
//import javax.servlet.http.HttpServlet; 
//import javax.servlet.*; 
import javax.servlet.http.*; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import java.sql.*; 
//import org.h2.jdbcx.JdbcConnectionPool; 

//import org.springframework.security.core.context.SecurityContextHolder; 


@Controller 
public class Java4sController {  
    HttpSession session; 
    String uname="",pass=""; 
    Statement stmt; 
    Connection conn; 

    @RequestMapping(value="displayForm", method=RequestMethod.GET) 
    public String helloWorld(UserDetails ud,HttpServletRequest request,HttpServletResponse response) { 
     return "loginPage";    
    } 

    @RequestMapping("/login") 
    public String loginCheck(@Valid UserDetails userDetails, BindingResult result, ModelMap model,HttpServletRequest request,HttpServletResponse response)throws Exception { 
     System.out.println("In the login page"); 
     String username=userDetails.getUser(); 
     String password=userDetails.getPassword(); 
     if (result.hasErrors()) { 
      return "loginPage"; 
     } 
     try { 
      Class.forName("org.h2.Driver"); 
      conn = DriverManager. 
       getConnection("jdbc:h2:file:C:/Users/aaggarwal/test", "sa", ""); 
      // add application code here 
      if(conn!=null) { 
       System.out.println("No connection"); 
      } 
      stmt=conn.createStatement(); 
      String sql="Select * FROM table1 WHERE username='"+username+"' AND password='"+password+"' "; 
      ResultSet rs=stmt.executeQuery(sql); 
      while(rs.next()) { 
       uname=rs.getString("username"); 
       pass=rs.getString("password"); 
       if (result.hasErrors()) { 
        return "loginPage"; 
       } else if(username.equals(uname)&&password.equals(pass)) { 
        //session.invalidate(); 
        response.setHeader("Cache-Control","no-cache"); 
        response.setHeader("Cache-Control","no-store"); 
        response.setDateHeader("Expires", 0); 
        response.setHeader("Pragma","no-cache"); 
        session = request.getSession(); // create session 
        session.setAttribute("username", username); 
        session.setAttribute("password", password); 
        //String str1=(String)session.getAttribute(username); 

        model.addAttribute("lfobj", userDetails); 
        //model.addAttribute(str1); 
        conn.close(); 
        return "success"; 
       } else { 
        conn.close(); 
        return "loginPage"; 
       } 
      } 
     } finally { 
      //finally block used to close resources 
      try{ 
       if(stmt!=null) 
       conn.close(); 
      } catch(SQLException se){ 
      }// do nothing 
      try{ 
      if(conn!=null) 
       conn.close(); 
      }catch(SQLException se){ 
       se.printStackTrace(); 
      }//end finally try 
     }//end try 
     return "abcd"; 
    } 

    @RequestMapping(value="logoutform", method=RequestMethod.GET) 
    public String helloWorld1(UserDetails ud,HttpServletRequest request,HttpServletResponse response) { 
     System.out.println("\nabcdef\n"); 
     response.setHeader("Cache-Control","no-cache"); 
     response.setHeader("Cache-Control","no-store"); 
     response.setDateHeader("Expires", 0); 
     response.setHeader("Pragma","no-cache"); 
     String username=ud.getUser(); 
     // String password=ud.getPassword(); 

     HttpSession session1; 
     session1=request.getSession(false); 
     System.out.println("Session :"+session1.getAttribute("username")); 
     if(session1!=null) { 
      //session1.removeAttribute(username); 
      session1.invalidate(); 
      Cookie[] cookies = request.getCookies(); 
      for (Cookie cookie : cookies) { 
       cookie.setMaxAge(0); 
       cookie.setValue(null); 
       cookie.setPath("/"); 
       response.addCookie(cookie); 
      } 
      System.out.println("invalidating session"); 
      //session1.invalidate(); 
     } 
     return "logoutsuccess";    
    } 
} 

java4s-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: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.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
<context:component-scan base-package="java4s" /> 
    <mvc:annotation-driven /> 

    <mvc:view-controller path="/" view-name="index"/> 


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="props" /> 
    </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/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4"> 
<servlet> 
    <servlet-name>java4s</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>java4s</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 
+0

更改爲'@RequestMapping(值= 「displayForm.html」' –

+0

我試過這有其他錯誤,現在像UserDetails無法解析到類型。 –

回答

0

錯誤是未找到具有URI的HTTP請求的映射[/LoginApp/displayForm.html]表示沒有爲displayForm.html映射。

由於控制器你給

@RequestMapping(value="displayForm", method=RequestMethod.GET) 

改變,要

@RequestMapping(value="displayForm.html", method=RequestMethod.GET) 

或者在web.xml中

<servlet-mapping> 
    <servlet-name>java4s</servlet-name> 
    <url-pattern>/*.html</url-pattern> 
</servlet-mapping> 
+0

@Archit Aggarwal確定什麼是UserDetails(來自Spring Security或您的bean類)並顯示錯誤 –

+0

我解決了錯誤,而沒有在代碼中做任何更改之前也發生過同樣的事情,有時代碼正在工作,有時它是不工作。它現在工作正常,甚至沒有改變displayform.html你可以幫助我與它。 –

+0

雅我可以幫助..然後是什麼實際問題? –

相關問題