我有这样的视图登录
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href='<c:url value = "/resources/images/favicon.png"/>'/>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href='<c:url value = "/resources/js/bootstrap/dist/css/bootstrap.css"/>'/>
<link rel="stylesheet" href='<c:url value = "/resources/fonts/font-awesome-4/css/font-awesome.min.css"/>'/>
<!-- Custom styles for this template -->
<link rel="stylesheet" href='<c:url value = "/resources/css/style.css"/>'/>
<link rel="stylesheet" type='text/css' href='<c:url value = "http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,400italic,700,800"/>'/>
<link rel="stylesheet" type='text/css' href='<c:url value = "http://fonts.googleapis.com/css?family=Raleway:300,200,100"/>'/>
</head>
<body class="texture">
<div id="cl-wrapper" class="login-container">
<div class="middle-login">
<div class="block-flat">
<div class="header">
<h3 class="text-center"><img class="logo-img" src="resources/images/logo.png" alt="logo"/>Halodoc Coupon</h3>
</div>
<div>
<form:form method="post" style="margin-bottom: 0px !important;" class="form-horizontal" action="j_spring_security_check" modelAttribute="users">
<div class="content">
<h4 class="title"><span style="color: red">${message}</span></h4>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" id="username" name="username" class="form-control" placeholder="Input Email" required>
</div> <!-- end input-group email-->
</div> <!-- end col-sm-12 email-->
</div><!-- end form-group email-->
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" id="password" name="password" placeholder="Password" required class="form-control">
</div> <!-- end input-group password-->
</div> <!-- end col-sm-12 password-->
</div><!-- end form-group password-->
</div> <!-- end content -->
<div class="foot">
<button class="btn btn-primary" data-dismiss="modal" type="submit" id="btnSubmit" value="Login">Login</button>
</div> <!-- end foot -->
</form:form>
</div>
</div> <!-- end block-flat -->
<div class="text-center out-links"><a href="#">© 2016 Polinasi Iddea Investama</a></div>
</div> <!-- end middle-login -->
</div><!-- end login container -->
<script type="text/javascript" src='<c:url value="/resources/js/jquery.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/behaviour/general.js"/>'></script>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" src='<c:url value="/resources/js/behaviour/voice-commands.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/bootstrap/dist/js/bootstrap.min.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/jquery.flot/jquery.flot.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/jquery.flot/jquery.flot.pie.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/jquery.flot/jquery.flot.resize.js"/>'></script>
<script type="text/javascript" src='<c:url value="/resources/js/jquery.flot/jquery.flot.labels.js"/>'></script>
</body>
</html>
然后我的控制器就像这样
@Controller
public class LoginController {
@Autowired
LoginDao loginDao;
@RequestMapping(value = { "/", "/home" })
public String getUserDefault() {
return "home";
}
@RequestMapping(value="/login", method = { RequestMethod.GET, RequestMethod.POST})
public ModelAndView getLoginForm(@ModelAttribute Users users,
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout, HttpServletRequest request) {
String message = "";
System.out.println(users.getUsername() + " | "+ users.getPassword() + " | " );
if (error != null) {
message = "Incorrect username or password !";
} else if (logout != null) {
message = "Logout successful !";
}
return new ModelAndView("login", "message", message);
}
@RequestMapping("/admin**")
public String getAdminProfile() {
return "admin";
}
@RequestMapping("/user**")
public String getUserProfile() {
return "user";
}
@RequestMapping("/403")
public ModelAndView getAccessDenied() {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
String username = "";
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
username = userDetail.getUsername();
}
return new ModelAndView("403", "username", username);
}
}
和我这样的春天安全
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 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/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/user**" access="hasRole('ROLE_USER')" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login login-page="/login" authentication-failure-url="/login?error"
default-target-url="/home" username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf />
</http>
<authentication-manager>
<authentication-provider user-service-ref="loginService" />
</authentication-manager>
我有问题显示值用户名和密码,我尝试总是空值,
@RequestMapping(value="/login", method = { RequestMethod.GET, RequestMethod.POST})
public ModelAndView getLoginForm(@ModelAttribute Users users,
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout, HttpServletRequest request) {
String message = "";
System.out.println(users.getUsername() + " | "+ users.getPassword() + " | " );
if (error != null) {
message = "Incorrect username or password !";
} else if (logout != null) {
message = "Logout successful !";
}
return new ModelAndView("login", "message", message);
}
如何从我的视图jsp获取值到我的控制器?
答案 0 :(得分:0)
而不是使用:
<input type="email" id="username" name="username" />
您应该使用spring form的输入taglib。例如:
<form:input path="email" id="username" .... />
详细了解Spring's tablib here,或者您可以阅读complete references here。
答案 1 :(得分:0)
更改此代码
@ModelAttribute Users users
到
@ModelAttribute("users") Users users
答案 2 :(得分:0)
实际上你要么从来没有用输入创建表单,要么你有输入但没有表单。问题出在这里:
如果您使用表单标记:
<form:form ...
您的输入还需要包含表单输入标记:
<form:input path="" value="" />
如果您不想使用表单标记,则只需声明这样的普通表单:
<form method="post" ...
正常输入如下:
<input name="" value="" />
并且不要忘记在@Controller中删除表单属性中的 modelAttribute , @ModelAttribute 。
在您的情况下,您现在使用 @ModelAttribute(“users”)绑定表单,但表单找不到表单输入的任何标记,表单实际上根本不发送任何输入。< / p>