更新JSP页面的内容,无需刷新
我有一个JSP网页,其中显示了一个表的内容。 当用户查看网页的上一第二逐第二基础的表的内容发生变化。 因此,用户每次看到新的和更新的内容,以刷新页面。 如何更新JSP页面的内容,而无需刷新页面。 我想要一个功能,就像在gmail.com那里的邮箱的大小只是不断没有增加用户耳目一新。
I have a jsp page which shows the contents of a table. While the user views a page the contents of the table changes on a second-by-second basis. So the user has to refresh the page every time to see fresh and updated contents. How can i update contents of the jsp page without having to refresh the page. I wanted a functionality just like in gmail.com where the size of the mailbox just keeps increasing without users refreshing.
您应该考虑使用Ajax(jQuery是我的preferred法)。
You should look into using Ajax (jQuery is my preferred method).
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.post/
这将然后打一个控制器,将返回所需的数据而无需刷新页面。
This would then hit a controller that would return the data you want without refreshing the page.
因此,举例来说,如果你有一个的login.jsp ...
So for instance, if you had a login.jsp...
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="true" %>
<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<head>
<title>Login</title>
</head>
<body>
<h1>
Hello please login to this application
</h1>
<script>
function login(){
var username = $("#username").val();
var password = $("#password").val();
$.post('login', { username : username , password : password }, function(data) {
$('#results').html(data).hide().slideDown('slow');
} );
}
</script>
Username : <input id="username" type="text" />
Password : <input id="password" type="password" />
<input name="send" type="submit" value="Click me" onclick="login()" />
<form name="next" action="auth/details" method="get">
<input name="send" type="submit" value="Go Through"/>
</form>
<div id="results" />
</body>
</html>
在控制器中,然后将打模式,但为了简单起见,我做了一个非常简单的例子...
In your controller, you would then hit the Model, but for simplicity, I've done a very simple example...
/**
* Handles requests for the application home page.
*/
@Controller
public class LoginController {
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
Util util;
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String home(Locale locale, Model model, String username, String password) {
if(username.equalsIgnoreCase("david"))
{
model.addAttribute("validUser", "Welcome " + username );
return "home";
}
else
{
model.addAttribute("validUser", "Incorrect username and password");
return "home";
}
}
}
这会再添加HTML缓慢滚动位到格说,如果它是有效的,在code产品低于...
This would then add a slow scrolling bit of html to the div to say if it's valid, the code for home is below...
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="true" %>
<html>
<body>
<P> ${validUser}. </P>
</body>
</html>