单击按钮时通过 javascript 调用 JSP 页面
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 的任何建议。
使用它,它将导航到 spring 控制器并显示所需页面
if(username == "admin" && password == "123")
{
document.location.href = "/home";
}
你可以在这里看到一个简单的例子:
https://dzone.com/articles/spring-mvc-example-for-user-registration-and-login-1
如果只想在JS中checked后显示首页,可以使用:
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")
。
我目前正在开发一个 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 的任何建议。
使用它,它将导航到 spring 控制器并显示所需页面
if(username == "admin" && password == "123")
{
document.location.href = "/home";
}
你可以在这里看到一个简单的例子: https://dzone.com/articles/spring-mvc-example-for-user-registration-and-login-1
如果只想在JS中checked后显示首页,可以使用:
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")
。