Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Cookies保存Spring MVC表单字段中的值_Java_Spring_Jsp_Spring Mvc_Cookies - Fatal编程技术网

Java 使用Cookies保存Spring MVC表单字段中的值

Java 使用Cookies保存Spring MVC表单字段中的值,java,spring,jsp,spring-mvc,cookies,Java,Spring,Jsp,Spring Mvc,Cookies,假设我有一个Spring MVC表单,如下所示: <form:form action="${pageContext.servletContext.contextPath}/secure/main.htm" commandName="secure/main"> <form:input path="operatorId" cssClass="textField"/> <form:input path="clientId" cssClass="text

假设我有一个Spring MVC表单,如下所示:

<form:form action="${pageContext.servletContext.contextPath}/secure/main.htm" commandName="secure/main">

    <form:input path="operatorId" cssClass="textField"/>

    <form:input path="clientId" cssClass="textField"/>

</form:form>
@RequestMapping(method = RequestMethod.POST)
public String processAuthenticate(@Valid AuthenticationForm authenticationForm,
                                  Map<String, Object> model,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

    authenticationForm = (AuthenticationForm) model.get("authenticationForm");

    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
            authenticationForm.setClientId(cookie.getValue());
        } else if (cookie.getName().equals("operatorId")) {
            authenticationForm.setOperatorId(cookie.getValue());
        }
    }

    String clientId = authenticationForm.getClientId();
    String operatorId = authenticationForm.getOperatorId();

    Cookie cookieClientId= new Cookie("clientId", clientId);
    cookieClientId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieClientId);

    Cookie cookieOperatorId = new Cookie("operatorId", operatorId);
    cookieOperatorId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieOperatorId);

    return MAIN_FORM_MAPPING;
}
@ModelAttribute("secure/" + MAIN_FORM_MAPPING)
public AuthenticationForm getAuthenticationForm() {
    return new AuthenticationForm();
}

但是,当我单击按钮并调用此方法时,我的值不会被保存。这是我第一次尝试使用
Cookies
,所以我可能遗漏了什么?我在回答这个问题。但在我的情况下,这是行不通的。有人能给我建议这个问题的解决方案吗?

请检查这是否有用。


对不起,误报了。我做的一切都是对的,但我有一个方法,在每个请求中,该方法将新的
AuthenticationForm
对象作为模型属性返回,如下所示:

<form:form action="${pageContext.servletContext.contextPath}/secure/main.htm" commandName="secure/main">

    <form:input path="operatorId" cssClass="textField"/>

    <form:input path="clientId" cssClass="textField"/>

</form:form>
@RequestMapping(method = RequestMethod.POST)
public String processAuthenticate(@Valid AuthenticationForm authenticationForm,
                                  Map<String, Object> model,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

    authenticationForm = (AuthenticationForm) model.get("authenticationForm");

    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
            authenticationForm.setClientId(cookie.getValue());
        } else if (cookie.getName().equals("operatorId")) {
            authenticationForm.setOperatorId(cookie.getValue());
        }
    }

    String clientId = authenticationForm.getClientId();
    String operatorId = authenticationForm.getOperatorId();

    Cookie cookieClientId= new Cookie("clientId", clientId);
    cookieClientId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieClientId);

    Cookie cookieOperatorId = new Cookie("operatorId", operatorId);
    cookieOperatorId.setMaxAge(COOKIE_EXPIRY);
    response.addCookie(cookieOperatorId);

    return MAIN_FORM_MAPPING;
}
@ModelAttribute("secure/" + MAIN_FORM_MAPPING)
public AuthenticationForm getAuthenticationForm() {
    return new AuthenticationForm();
}
我有一个方法,用于在视图中显示表单:

@RequestMapping(method = {RequestMethod.GET})
public String showForm(Map<String, Object> model, HttpServletRequest request) {
    AuthenticationForm authenticationForm = (AuthenticationForm) model.get("secure/main");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getValue());
        if (cookie.getName().equals("clientId")) {
           authenticationForm.setClientId(cookie.getValue());   //Just added this code to this method
        } else if (cookie.getName().equals("operatorId")) {
           authenticationForm.setOperatorId(cookie.getValue());
        }
    }
    return MAIN_FORM_MAPPING;
}
@RequestMapping(方法={RequestMethod.GET})
公共字符串showForm(映射模型,HttpServletRequest){
AuthenticationForm AuthenticationForm=(AuthenticationForm)model.get(“secure/main”);
Cookie[]cookies=request.getCookies();
用于(Cookie:cookies){
System.out.println(cookie.getValue());
if(cookie.getName().equals(“clientId”)){
authenticationForm.setClientId(cookie.getValue());//刚刚将此代码添加到此方法中
}else if(cookie.getName().equals(“运算符ID”)){
authenticationForm.setOperatorId(cookie.getValue());
}
}
返回主窗体映射;
}

然后我意识到这个表单对象总是新的,所以我从cookies设置的值总是在一个新表单上。我必须在
showForm
方法中设置cookie中的值,一切正常。

我直接将cookie添加到响应中,但我返回的是视图映射,而不是
ModelAndView
。这有什么区别吗?我不这么认为……在cookie中存储数据的方式存在安全隐患——是否安全,是否有人可以编辑他们的cookie并查看其他人的数据,等等?另一种方法是在cookie中存储一个(散列?)键,然后使用该键从数据库中检索数据。好的建议,谢谢,我会这样做的。