我有這樣如何從JSP獲得價值到控制器
<%@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到我的控制器的價值?
iam已經使用,但我得到了錯誤HTTP狀態500 -/WEB-INF /頁/登錄。jsp(line:40,column:122)expected symbol expected – unknown
「等符號預期」主要是由於您在添加JSP指令,聲明,標記庫等時忘記添加'='符號引起的。輸入.... />'在這裏,我們可以從那裏調查。 – xsalefter