2016-03-05 56 views
0

對不起,如果我對此很愚蠢。阿賈克斯在回覆後返回主頁

我只是嘗試功能之前,有一個適當的結構。

在輸入頁面點擊登錄時,它應該調用一個ajax jsp。 我將它打印在警報中進行驗證。 打印完成後。它回到主頁。

這是我的歡迎頁面。 http://localhost:8080/Example/

報警後,它可以追溯到 http://localhost:8080/Example/?

我試了一下Spring MVC中

Spring_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-3.0.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-3.0.xsd 
    "> 
     <context:component-scan base-package="com.ksv" /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" 
    cache-period="31556926"/> 
    <mvc:annotation-driven /> 

</beans> 

控制器

package com.ksv; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class Hello { 
    @RequestMapping("/") 
    public ModelAndView helloWorld() { 
     String message = "HELLO SPRING MVC HOW R U"; 
     System.out.println("454545"); 

     return new ModelAndView("index"); 
    } 


    @RequestMapping("/loajax") 
    public ModelAndView helloajax(HttpServletRequest request,HttpServletResponse res) 
    { 
     System.out.println("hjhjhjh"); 

     return new ModelAndView("loajax"); 
    } 
} 

JSP

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Calm breeze login screen</title> 
<link rel="shortcut icon" 
    href="${pageContext.request.contextPath}/resources/logo.ico"> 
<link rel="stylesheet" 
    href="${pageContext.request.contextPath}/resources/css/style.css"> 
<script> 
    function doAjaxPost() {  
    $.ajax({ 
    type : "Get", 
    url : "loajax", 

    success : function(res) { 
     alert(res); 
    }, 
    error : function(e) { 
     alert('Error: ' + e); 
    } 
    }); 

    } 
    </script> 
</head> 
<body> 
    <div class="wrapper"> 
     <div class="container"> 
      <h1>Welcome</h1> 
      <br> 
      <form name="vinform"> 
       <input type="text" placeholder="Username"><br> 
       <button id="login-button" onClick="doAjaxPost()">Login</button> 
       <br> 
       <h2> 
        <a href="inda.html">Create Account</a> 
       </h2> 
       <a href="inda.html">Forgot?</a><br> <br> <br> <span 
        id="ksv"> </span> 

       <div class="img" align="center"></div> 
       <h3>This area is used to describe something which can be later 
        decided</h3> 

      </form> 
     </div> 

     <ul class="bg-bubbles"> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
      <li></li> 
     </ul> 
    </div> 
    <script 
     src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

    <script src="${pageContext.request.contextPath}/resources/js/index.js"></script> 




</body> 
</html> 
+0

有更多的代碼可以提供嗎? – wong2

+0

@ wong2我已添加。 – ksv

+0

你傳遞給ajax請求的URL是錯誤的......它應該以「http:// localhost:8080/example/loajax」開頭。 控制器/ loajax的語法對於ajax調用有點不同。請參考[this](http://www.mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example/) )簡單的彈簧mvc ajax控制器的鏈接 – damitj07

回答

0

關於Ajax你想改變頁面? 通常情況下,您返回json或xml(只有數據realetd不html的東西)東西上的ajax返回方法。我可以建議你,清潔的解決方案使用註釋@RestController另一個控制器,這裏定義loadAjax功能,並返回字符串出現,它會工作

@RestController 
public class AjaxHandler { 

    @RequestMapping("/loajax") 
    public String serveAjax(HttpServletRequest request,HttpServletResponse res) 
    { 
     System.out.println("hjhjhjh"); 
     return "loajax"; 
    } 
} 

對於簡單和概念的理解我給這種類型的例子。其他方面,你也可以做同樣的控制器。 希望它有幫助。