2015-10-19 51 views
2

如何保持文本值,因为它是后点击提交按钮

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.jspLoginController.java 1)我想要的文本值应保持的代码,因为它是后提交按钮点击。 假设用户输入了错误的密码,并且他点击了提交按钮,所以用户ID不应该消失。用户标识应保持原样,并且密码错误。我已经在jsp中为每个字段添加了值= "<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>">。但是这不起作用,因为在控制器中进行验证。

2)在LoginController.java文件中,我有验证哪些不起作用。如何解决它们?这是服务器端验证。我有jQuery中的客户端验证。

+0

我认为支撑豆的使用会更有帮助, bindingResult.hasError()可以用来做任何事情,如果出现错误,并且后台bean将负责为你保留值 – flipper

+0

它是可以的。 1)但是我不能在上面的代码中进行更改吗?我是否需要更改整个代码?我不知道什么是支持豆类。 2)请给我任何与我的代码有关的相关链接吗?我更喜欢第一个问题的答案。 – rucha

+0

希望这个论坛的讨论将帮助你http://bytes.com/topic/java/answers/867692-how-retain-username-text-box – flipper

回答

1

您可以将控制器中收到的所有属性添加到会话属性中,以便您的属性和值将保留在login.jsp中。

在Spring参考此链接,使用@SessionAttributes

http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/

或者,你可以使用Spring表单支持对象,并将其存储在会话

参考,此链接,使用弹簧表单支持对象

http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm

+0

好的,谢谢你,先生,会尝试使用这两个链接。 – rucha

+0

如果您发现此解决方案有帮助,敬请投票! – Prem

+0

对不起,先生。我无法做到这一点,因为我没有赢得总共15个声誉。 – rucha

相关问题