阿贾克斯在回复后返回主页
问题描述:
对不起,如果我对此很愚蠢。阿贾克斯在回复后返回主页
我只是尝试功能之前,有一个适当的结构。
在输入页面点击登录时,它应该调用一个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>
答
关于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";
}
}
对于简单和概念的理解我给这种类型的例子。其他方面,你也可以做同样的控制器。 希望它有帮助。
有更多的代码可以提供吗? – wong2
@ wong2我已添加。 – ksv
你传递给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