2016-04-06 88 views
1

我有這樣如何從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="#">&copy; 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

而不是使用:

<input type="email" id="username" name="username" /> 

你應該用彈簧形式的輸入標籤庫來代替。例如:

<form:input path="email" id="username" .... /> 

瞭解更多關於Spring's tablib here,或者你可以閱讀complete references here

+0

iam已經使用,但我得到了錯誤HTTP狀態500 -/WEB-INF /頁/登錄。jsp(line:40,column:122)expected symbol expected – unknown

+0

「等符號預期」主要是由於您在添加JSP指令,聲明,標記庫等時忘記添加'='符號引起的。輸入.... />'在這裏,我們可以從那裏調查。 – xsalefter

0

改變此密碼

@ModelAttribute Users users 

@ModelAttribute("users") Users users 
0

其實你要麼從未創建形式輸入,或者你有一個輸入,但不具有的形式。問題就在這裏:

如果使用形式標籤:

<form:form ... 

您的輸入還需要包括形式輸入標籤:

<form:input path="" value="" /> 

如果你不想要使用表格標記,您只需聲明像這樣的標準格式:

<form method="post" ... 

和正常輸入這樣的:

<input name="" value="" /> 

不要忘記刪除的ModelAttribute在形式上還屬性@ModelAttribute在@Controller。

在你的情況,現在,你要綁定的形式@ModelAttribute(「用戶」)但形式犯規找到表單輸入任何標籤和形式實際上不發送輸入可言。