[英]Call a JSP page through javascript on button click
我目前正在开发一个MVC程序,当用户从login.jsp
登录时,需要调用home.jsp
页面。 我已经为呈现视图的登录和家庭控制器编写了代码。 但是不确定如何通过我的资源下的custom.js
javascript文件调用控制器的请求映射。
custom.js
document.getElementById('login_btn').addEventListener("click", function() {
authorizationCheck();
}, false);
function authorizationCheck()
{
var username = document.getElementById('username_txt').value;
var password = document.getElementById('password_txt').value;
if(username == "admin" && password == "123")
{
//code to load the home.jsp
}
}
HomeController.java
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String login(Locale locale, Model model) {
return "login";
}
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
}
我对这个框架很陌生。 因此,有关如何在单击按钮时加载不同的jsp的任何建议将受到高度赞赏。
您可以在此处看到一个简单的示例: https : //dzone.com/articles/spring-mvc-example-for-user-registration-and-login-1
如果只想在签入JS后显示主页,则可以使用:
if(username == "admin" && password == "123")
{
document.location.href = "home";
}
使用它,它将导航到弹簧控制器并显示所需的页面
if(username == "admin" && password == "123")
{
document.location.href = "/home";
}
您的表格可能很简单(JSP的一部分)
<form id="myForm" method="post" action="http://localhost/your_app_root/check_credentials.jsp">
<input id="username-txt" name = "username" type="text" value="some_username">
<input id="password-txt" name = "password" type="text" value="some_password">
<input type="submit" value="Log in">
</form>
在JS中
function authorizationCheck()
{
//do some processing if necessary (you can encrypt username/psw but this is only slightly more secure)
document.getElementById("myForm").submit();
}
在您的控制器(是Spring)中,您将检索用户名和密码作为@RequestParam
参数。
在纯JSP(JSP / servlet)中,分别使用request.getParameter("username")
和request.getParameter("password")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.