Spring On Submit按钮文本框值保持不变但选择标记变空

时间:2018-04-26 12:18:24

标签: javascript java spring spring-mvc

这是我的PojoClass

package com.LoginPojo;

    import org.hibernate.validator.constraints.NotEmpty;

    public class LoginDeatils {
    public String state;

    public String district;

    public String userName;



    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }



    }

这是我的JSP文件: -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
            <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form:form action="login" method="POST" commandName="loginBean" accept-charset="UTF-8">  
        <table> 
        <tr>
            <td>Name:<form:input id="userName" name="userName" path="userName" /><br></td>
             <td align="left"><form:errors path="userName" cssClass="error"/></td>
        </tr>

            <tr>
                <td>State: <form:select path="state" items="${stateList}" onchange="addDistrictValues()"  /><br></td>
            </tr>

            <tr>
                <td>District: <form:select path="district"    /><br></td>

            </tr>

        </table>
        <input type="submit" value="login"/>

    </form:form>
        <script type="text/javascript">

            function addDistrictValues() {
                 document.getElementById("userName").value="abchd";    

                 var x = document.getElementById("district");
                 var option = document.createElement("option");
                 option.text = "Raigad";
                 x.add(option); 


            }
            </script>

    </body>
    </html>

这是我的控制器:

package com.SelectTagPackage;

    import java.util.ArrayList;
    import java.util.List;

    import javax.validation.Valid;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    import com.LoginPojo.LoginDeatils;
    import com.validator.LoginValidator;

    @Controller
    @RequestMapping("/login")
    public class SelectTagController {

        @Autowired
        LoginValidator loginValidator;

        public LoginValidator getLoginValidator() {
            return loginValidator;
        }

        public void setLoginValidator(LoginValidator loginValidator) {
            this.loginValidator = loginValidator;
        }

        @RequestMapping (method =  RequestMethod.POST)
         public ModelAndView loginPOST(@Valid @ModelAttribute("loginBean")LoginDeatils loginBean , BindingResult bindingResult,ModelMap map) throws Exception 
         {  
            loginValidator.validate(loginBean, bindingResult);
             if (bindingResult.hasErrors()) {           
                 return  new ModelAndView("pageForSelectTag"); //JSP Page Name
                }
             else
             {
                 System.out.println("bindingResult.hasErrors() else block");
           return  new ModelAndView("pageForSelectTag"); //JSP Page Name
             }
         }

        @RequestMapping (method =  RequestMethod.GET)
         public ModelAndView loginGET(@Valid @ModelAttribute("loginBean")LoginDeatils loginBean , BindingResult bindingResult,ModelMap map) throws Exception 
         {


           return  new ModelAndView("pageForSelectTag");
         }


        @ModelAttribute("stateList")
        public List loadprofessionList() {
             List<String> stateList = new ArrayList<String>();
             stateList.add("Maharashtra");
             stateList.add("Keral");

              return stateList;
        }

    }

这是我的验证员类: -

package com.validator;

    import org.springframework.stereotype.Component;
    import org.springframework.validation.Errors;
    import org.springframework.validation.ValidationUtils;
    import org.springframework.validation.Validator;

    import com.LoginPojo.LoginDeatils;



    @Component
    public class LoginValidator   implements Validator {

        public boolean supports(Class<?> arg0) {
            // TODO Auto-generated method stub
            return LoginDeatils.class.isAssignableFrom(arg0);
        }

        public void validate(Object arg0, Errors errors) {

            LoginDeatils objLoginDeatils=(LoginDeatils)arg0;

            if(!objLoginDeatils.getUserName().equals("milind"))
            {           
                 ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "error.userName", "userName is requiredddd.");

            }

        }

    }

我的问题是: - 每当我执行onchange事件状态组合框2事情就会发生

<td>State: <form:select path="state" items="${stateList}" onchange="addDistrictValues()"  /><br></td>

1)addDistrictValues javascript方法被调用。

function addDistrictValues() {
             document.getElementById("userName").value="abchd";    

             var x = document.getElementById("district");
             var option = document.createElement("option");
             option.text = "Raigad";
             x.add(option); 


        }

将abchd值设置为userName文本框参考: -

document.getElementById("userName").value="abchd";

和第二个Raigad值将被添加到区域选择框JSP代码中: -

<td>District: <form:select path="district"    /><br></td>

javascript代码: -

var x = document.getElementById("district");
             var option = document.createElement("option");
             option.text = "Raigad";
             x.add(option);

但每当我点击提交按钮i。 e

<input type="submit" value="login"/>

调用控制器loginPOST方法,内部调用validate方法并重新加载表单

@RequestMapping (method =  RequestMethod.POST)
     public ModelAndView loginPOST(@Valid @ModelAttribute("loginBean")LoginDeatils loginBean , BindingResult bindingResult,ModelMap map) throws Exception 
     {  
        loginValidator.validate(loginBean, bindingResult);
         if (bindingResult.hasErrors()) {           
             return  new ModelAndView("pageForSelectTag"); //JSP Page Name
            }
         else
         {
             System.out.println("bindingResult.hasErrors() else block");
       return  new ModelAndView("pageForSelectTag"); //JSP Page Name
         }
     }

但是当使用validate方法调用文本框值“abchd”的控制器方法仍然保留在文本框上时,我们通过javascript添加的“Raigad”值[文本框值也由javascript设置]将从选择框中消失

so why only select box value only getting vanish but textbox value remains unchange , both are setting by javascript.

plz help 

enter image description here

enter image description here

enter image description here

我在谷歌搜索更多,但我没有从任何地方得到任何适当的解决方案

但是当使用validate方法调用文本框值“abchd”的控制器方法仍然保留在文本框上时,我们通过javascript添加的“Raigad”值[文本框值也由javascript设置]将从选择框中消失

所以为什么IN SPRING只选择框值才会消失但文本框值仍然不变,两者都是通过javascript设置的。

一旦你看到附件中的图片,你就会明白怀疑

plz help

1 个答案:

答案 0 :(得分:0)

您没有提供&#34;区&#34;的项目列表。选择控制就像你正在做的那样#34;州&#34;选择控制。在您的控制器中尝试此操作:

    @ModelAttribute("districtMap")
    public List loadDistrictMap() {
         Map<String, String> districts new LinkedHashMap<String, String>();
         districts.put("1", "1st District");
         districts.put("2", "2nd District");
         districts.put("3", "Raigad");
         districts.put("n", "Nth District");

          return districts;
    }

然后在您的JSP表单中:

<td>District: <form:select path="district" items="${districtMap}"    /><br></td>

区域地图只是一个示例列表,它可能来自数据库或其他东西,但重点是您的控制器应以某种方式提供可能的区域选项的完整列表,以便选择控件可以基于&#34;路径&#34;属性。