单击提交按钮后如何保持文本值

时间:2015-10-19 07:12:10

标签: java jquery html jsp

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
{	
	@RequestMapping(value="/login.htm", method = RequestMethod.GET)
	public String GetL(Map<String, Object> model){
		return "login";
	}
	
	private Map<String, String> LoginMap = new HashMap<String, String>();
	 
    public Map<String, String> getLoginMap() {
        return LoginMap;
    }
 
    public void setLoginMap(Map<String, String> LoginMap) {
        this.LoginMap = LoginMap; 
        }
	
	@RequestMapping(value="/login.htm", method = RequestMethod.POST)

	public String login(@RequestParam(value="userid", required=true) String userid,
						@RequestParam(value="password", required=true) String password,
						@RequestParam(value="confirmpassword", required=true) String confirmpassword,
						@RequestParam(value="role", required=true) String role,
						Map<String, Object> model, ServletRequest request)

{
		if(userid.matches("^[a-zA-Z0-9]{5,24}$") && password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{5,15}$")
		&& confirmpassword.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{6,20}$") 
		&& (role.equals(new String("OPS(Operational)"))||role.equals(new String("Helpdesk"))))
		
		{
    		model.put("userid", userid);
    		model.put("password", password);
    		model.put("confirmpassword", confirmpassword);
    		model.put("role", role);
    		
    		System.out.println("successful!");
    		return "page2";
		}
		else
		{
			boolean validated = true;
			getLoginMap();
			String errors ="";
			if(userid.isEmpty())
			{
				validated = false;
				model.put("useridError","Please enter user id"); 
				System.out.println("executed");
				errors = "useridError&"; 
			}
			if(password.isEmpty())
			{
				validated = false;
				model.put("passwordError","Please enter password");
				System.out.println("executed2");
				errors.concat("passwordError&");
			} 
			if(confirmpassword.isEmpty())
			{
				validated = false;
				model.put("confirmpasswordError","Please enter confirmpassword");
				System.out.println("executed3");
				errors.concat("confirmpasswordError&");
			} 
			if(role==null || role.isEmpty())
			{
				validated = false;
				model.put("roleError","Please select one");
				System.out.println("executed4");
				errors.concat("roleError&");
			} 
			
			if(validated)
			{
				return "page2";
			}
			else
			{
				return "redirect:login.htm?"+ errors; 
			}
		}	
	}
}
<!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%">
		<table class="tableprop" id="tableform" border="0" width="0" cellspacing="5" cellpadding="5">
		
		<h3> Add a new user </h3>
		
			<tr>
				<td align="center">User ID:</td>
				<td><input tabindex="5" size="20" type="text"  name="userid" id="userid"  value="<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>"></td>
				<td><c:if test="${param.useridError != null}">
						<div id="error" class="alert alert-danger">
						    <font color="red"><p>Invalid UserId</p></font>
						</div></c:if></td>
			</tr>
			
			<tr>
				<td align="center">Password:</td>
				<td><input tabindex="5" size="20" type="password" name="password" id="password" value="<%=request.getParameter("password")!=null?request.getParameter("password"):""%>"></td>
				<td><c:if test="${param.passwordError != null}">
						<div class="alert alert-danger">
						 <font color="red"><p align="inline" class="err">Incorrect Password</p></font>
						</div></c:if></td>
			</tr>
			
			<tr>
				<td align="center">Confirm Password:</td>
				<td><input tabindex="5" size="20" type="password" name="confirmpassword" id="confirmpassword" value="<%=request.getParameter("confirmpassword")!=null?request.getParameter("confirmpassword"):""%>"></td>
					<td><c:if test="${param.confirmpasswordError != null}">
						<div class="alert alert-danger">
					<font color="red"><p>Password should be same as above</p></font>
						</div></c:if></td>
			</tr>
			
			<tr>
				<td align="center">Role:</td>
				<td><select name="role" id="role" title="Please select role" tabindex="5" value="<%=request.getParameter("role")!=null?request.getParameter("role"):""%>"></td>
					<option value="">Select a specific role</option>
					<option value="OPS(Operational)">OPS(Operational)</option>
					<option value="Helpdesk">Helpdesk</option>
					</td>
					<td><c:if test="${param.roleError != null}">
						<div class="alert alert-danger">
						    <font color="red"><p>Please select one</p></font>
						</div></c:if></td>
					</select>	
			</tr>
			
			<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
			
			<tr>
				<td align="center" colspan="4"><input tabindex="7" type="submit" value="Submit" id="submit" class="submit"/></td>					
			</tr>

			</table>				
		</form>
	</div>
<script>
	// just for the demos, avoids form submit
	jQuery.validator.setDefaults({
	debug: true,
	success: "valid"
	});
</script>
</body>
</html>

这两个是代码login.jspLoginController.java 1)我希望文本值保持原样,单击提交按钮后。 假设用户输入了错误的密码,他点击了提交按钮,因此用户ID不会消失。用户ID应保持原样,并且消息应该是密码错误。我在jsp中为每个字段添加了值= "<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>">。但这不起作用,因为控制器中的验证。

2)在LoginController.java文件中,我的验证无效。如何解决它们?这是服务器端验证。我在jquery中也有客户端验证。

2 个答案:

答案 0 :(得分:1)

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

请参阅此链接以在Spring中使用@SessionAttributes

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

或者,您可以使用spring form backing对象并将其存储在会话

引用此链接以使用弹簧形式支持对象

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

答案 1 :(得分:0)

您可以添加到请求属性中并像这样enter image description here那样检索enter image description here