import java.util.Map;
import java.util.HashMap;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController
{ \t
\t @RequestMapping(value="/login.htm", method = RequestMethod.GET)
\t public String GetL(Map<String, Object> model){
\t \t return "login";
\t }
\t
\t private Map<String, String> LoginMap = new HashMap<String, String>();
\t
public Map<String, String> getLoginMap() {
return LoginMap;
}
public void setLoginMap(Map<String, String> LoginMap) {
this.LoginMap = LoginMap;
}
\t
\t @RequestMapping(value="/login.htm", method = RequestMethod.POST)
\t public String login(@RequestParam(value="userid", required=true) String userid,
\t \t \t \t \t \t @RequestParam(value="password", required=true) String password,
\t \t \t \t \t \t @RequestParam(value="confirmpassword", required=true) String confirmpassword,
\t \t \t \t \t \t @RequestParam(value="role", required=true) String role,
\t \t \t \t \t \t Map<String, Object> model, ServletRequest request)
{
\t \t if(userid.matches("^[a-zA-Z0-9]{5,24}$") && password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{5,15}$")
\t \t && confirmpassword.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{6,20}$")
\t \t && (role.equals(new String("OPS(Operational)"))||role.equals(new String("Helpdesk"))))
\t \t
\t \t {
\t \t model.put("userid", userid);
\t \t model.put("password", password);
\t \t model.put("confirmpassword", confirmpassword);
\t \t model.put("role", role);
\t \t
\t \t System.out.println("successful!");
\t \t return "page2";
\t \t }
\t \t else
\t \t {
\t \t \t boolean validated = true;
\t \t \t getLoginMap();
\t \t \t String errors ="";
\t \t \t if(userid.isEmpty())
\t \t \t {
\t \t \t \t validated = false;
\t \t \t \t model.put("useridError","Please enter user id");
\t \t \t \t System.out.println("executed");
\t \t \t \t errors = "useridError&";
\t \t \t }
\t \t \t if(password.isEmpty())
\t \t \t {
\t \t \t \t validated = false;
\t \t \t \t model.put("passwordError","Please enter password");
\t \t \t \t System.out.println("executed2");
\t \t \t \t errors.concat("passwordError&");
\t \t \t }
\t \t \t if(confirmpassword.isEmpty())
\t \t \t {
\t \t \t \t validated = false;
\t \t \t \t model.put("confirmpasswordError","Please enter confirmpassword");
\t \t \t \t System.out.println("executed3");
\t \t \t \t errors.concat("confirmpasswordError&");
\t \t \t }
\t \t \t if(role==null || role.isEmpty())
\t \t \t {
\t \t \t \t validated = false;
\t \t \t \t model.put("roleError","Please select one");
\t \t \t \t System.out.println("executed4");
\t \t \t \t errors.concat("roleError&");
\t \t \t }
\t \t \t
\t \t \t if(validated)
\t \t \t {
\t \t \t \t return "page2";
\t \t \t }
\t \t \t else
\t \t \t {
\t \t \t \t return "redirect:login.htm?"+ errors;
\t \t \t }
\t \t } \t
\t }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="include.jsp" %>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div align="center" id='formlogin' class="container">
<form method="post" id="loginForm" name="loginForm" action="login.htm" commandName="userForm" style="margin-bottom:30%">
\t \t <table class="tableprop" id="tableform" border="0" width="0" cellspacing="5" cellpadding="5">
\t \t
\t \t <h3> Add a new user </h3>
\t \t
\t \t \t <tr>
\t \t \t \t <td align="center">User ID:</td>
\t \t \t \t <td><input tabindex="5" size="20" type="text" name="userid" id="userid" value="<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>"></td>
\t \t \t \t <td><c:if test="${param.useridError != null}">
\t \t \t \t \t \t <div id="error" class="alert alert-danger">
\t \t \t \t \t \t <font color="red"><p>Invalid UserId</p></font>
\t \t \t \t \t \t </div></c:if></td>
\t \t \t </tr>
\t \t \t
\t \t \t <tr>
\t \t \t \t <td align="center">Password:</td>
\t \t \t \t <td><input tabindex="5" size="20" type="password" name="password" id="password" value="<%=request.getParameter("password")!=null?request.getParameter("password"):""%>"></td>
\t \t \t \t <td><c:if test="${param.passwordError != null}">
\t \t \t \t \t \t <div class="alert alert-danger">
\t \t \t \t \t \t <font color="red"><p align="inline" class="err">Incorrect Password</p></font>
\t \t \t \t \t \t </div></c:if></td>
\t \t \t </tr>
\t \t \t
\t \t \t <tr>
\t \t \t \t <td align="center">Confirm Password:</td>
\t \t \t \t <td><input tabindex="5" size="20" type="password" name="confirmpassword" id="confirmpassword" value="<%=request.getParameter("confirmpassword")!=null?request.getParameter("confirmpassword"):""%>"></td>
\t \t \t \t \t <td><c:if test="${param.confirmpasswordError != null}">
\t \t \t \t \t \t <div class="alert alert-danger">
\t \t \t \t \t <font color="red"><p>Password should be same as above</p></font>
\t \t \t \t \t \t </div></c:if></td>
\t \t \t </tr>
\t \t \t
\t \t \t <tr>
\t \t \t \t <td align="center">Role:</td>
\t \t \t \t <td><select name="role" id="role" title="Please select role" tabindex="5" value="<%=request.getParameter("role")!=null?request.getParameter("role"):""%>"></td>
\t \t \t \t \t <option value="">Select a specific role</option>
\t \t \t \t \t <option value="OPS(Operational)">OPS(Operational)</option>
\t \t \t \t \t <option value="Helpdesk">Helpdesk</option>
\t \t \t \t \t </td>
\t \t \t \t \t <td><c:if test="${param.roleError != null}">
\t \t \t \t \t \t <div class="alert alert-danger">
\t \t \t \t \t \t <font color="red"><p>Please select one</p></font>
\t \t \t \t \t \t </div></c:if></td>
\t \t \t \t \t </select> \t
\t \t \t </tr>
\t \t \t
\t \t \t <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
\t \t \t
\t \t \t <tr>
\t \t \t \t <td align="center" colspan="4"><input tabindex="7" type="submit" value="Submit" id="submit" class="submit"/></td> \t \t \t \t \t
\t \t \t </tr>
\t \t \t </table> \t \t \t \t
\t \t </form>
\t </div>
<script>
\t // just for the demos, avoids form submit
\t jQuery.validator.setDefaults({
\t debug: true,
\t success: "valid"
\t });
</script>
</body>
</html>
這兩個是login.jsp
和LoginController.java
1)我想要的文本值應保持的代碼,因爲它是後提交按鈕點擊。 假設用戶輸入了錯誤的密碼,並且他點擊了提交按鈕,所以用戶ID不應該消失。用戶標識應保持原樣,並且密碼錯誤。我已經在jsp中爲每個字段添加了值= "<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>">
。但是這不起作用,因爲在控制器中進行驗證。
2)在LoginController.java
文件中,我有驗證哪些不起作用。如何解決它們?這是服務器端驗證。我有jQuery中的客戶端驗證。
我認爲支撐豆的使用會更有幫助, bindingResult.hasError()可以用來做任何事情,如果出現錯誤,並且後臺bean將負責爲你保留值 – flipper
它是可以的。 1)但是我不能在上面的代碼中進行更改嗎?我是否需要更改整個代碼?我不知道什麼是支持豆類。 2)請給我任何與我的代碼有關的相關鏈接嗎?我更喜歡第一個問題的答案。 – rucha
希望這個論壇的討論將幫助你http://bytes.com/topic/java/answers/867692-how-retain-username-text-box – flipper