Ajax在回复后返回主页

时间:2016-03-05 10:34:31

标签: javascript jquery html ajax spring

很抱歉,如果我这么愚蠢的话。

我只是在拥有合适的结构之前尝试功能。

在点击登录时的输入页面中,它应该调用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>

1 个答案:

答案 0 :(得分:0)

关于Ajax的

你想改变页面吗? 通常你在ajax返回方法上返回json或xml(只有数据realetd而不是html有点东西)。我可以建议你,对于干净的解决方案,使用注释@RestController创建另一个控制器,在这里定义loadAjax函数,并在那里返回String,它将起作用

public static String printKeyHash(Activity context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        //getting application package name, as defined in manifest
        String packageName = context.getApplicationContext().getPackageName();

        //Retriving package info
        packageInfo = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);

        Log.e("Package Name=", context.getApplicationContext().getPackageName());

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = new String(Base64.encode(md.digest(), 0));

            // String key = new String(Base64.encodeBytes(md.digest()));
            Log.e("Key Hash=", key);
        }
    } catch (PackageManager.NameNotFoundException e1) {
        Log.e("Name not found", e1.toString());
    }
    catch (NoSuchAlgorithmException e) {
        Log.e("No such an algorithm", e.toString());
    } catch (Exception e) {
        Log.e("Exception", e.toString());
    }

    return key;
}

为了简单和概念理解,我已经给出了这种类型的例子。另外,你也可以做同样的控制器。 希望它有所帮助。