【问题标题】:JSP (hidden field) Jquery spring -simpleurlhandlermappingJSP(隐藏字段)Jquery spring -simpleurlhandlermapping
【发布时间】:2018-01-28 16:16:15
【问题描述】:

寻求您的帮助。

问题:在高层次上,问题是我们在 jsp 中从 java 脚本中设置的隐藏字段正在获取模型对象。

1- 我是 JSP 2-具有扩展多动作控制器的控制器 3-模型对象

我的 JS:

ARD.save = function(url){
    var query = "?method=save";
    var jsonArray = [];
    $('#submitbutton').click(function() {

        $("input:checkbox:checked").each(function(){
            var jsonObj = {};
            jsonObj.id = $(this).attr("id");
            /* jsonObj.name = $(this).attr("value"); */
            jsonArray.push(jsonObj);
        });
        alert (" all values - "+JSON.stringify(jsonArray,0,"\t"));

        document.getElementById('selectedIds').setAttribute('value', JSON.stringify(jsonArray,0,"\t"));
        alert ("values1 - "+document.getElementById('selectedIds').value);
        $('#result').text(document.getElementById('selectedIds').value);
    });

JSP:
<form method="POST"  name="xxxxx" id="xxxxxx">
    <h1>testing started.</h1>
    <%-- <input type="hidden" id="selectedIds" value="${model.setlectedValue}" /> --%>

    <input type="hidden" id="selectedIds" id="selectedIds" value="" />

    <table border="1" name="myTable" id="myTable">
        <thead>
            <tr>
                <th><input type="checkbox" id="selectall" />SelectAll</th>
                <th>ID</th>
                <th>Name</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="value" items="${model.values}">
                <tr>
                    <td align="center"><input type="checkbox" class="case"
                        id="${value.empid}" name="agreementNumber" value="${value.empid}" /></td>
                    <td>${value.empid}</td>
                    <td>${value.name}</td>
                    <td>${value.date}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>

    <input type="button" name="submitbutton" id="submitbutton"  value="submit Button" 
    onclick="ARD.save('<c:url value="/tbletest.htm" />');" />

</form>

<div  id="result">

Controller :
public class LoginController extends MultiActionController {  //SimpleFormController{ //

    /*public LoginController(){
        setCommandClass(Login.class);
        setCommandName("loginForm");
    }*/


    private TableModel model;

    public TableModel getModel() {
        return model;
    }

    public void setModel(TableModel model) {
        this.model = model;
    }

    public LoginController(){
        model=new TableModel();
    }

    //@Override
    public ModelAndView save(HttpServletRequest request,    HttpServletResponse response) throws Exception {
        //Login login = (Login) command;
        //return new ModelAndView("loginSuccess","login",login);

         System.out.println("save method.");
         System.out.println(" model.getSetlectedValue() -> "+ model.getSetlectedValue());

         System.out.println("request object : - "+request.getAttribute("selectedIds"));
         System.out.println(" request.getParameter(\"selectedIds\") : - "+ request.getParameter("selectedIds"));
         System.out.println(" model stestvalue: - "+ model.getTestValue());
        //return new ModelAndView("loginSuccess");
        //return new ModelAndView("tbletest");
        return new ModelAndView("tbletest","model",model);

    }

型号:

public class TableModel {

    private List<TableValues>  values;

    private Object setlectedValue;


    private String testValue;

spring 配置的应用上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/welcome.htm">welcomeController</prop>
                <prop key="/helloWorld.htm">helloController</prop>
                <prop key="/tbletest.htm">loginController</prop>

            </props>
        </property>
    </bean>

    <bean id="hello`enter code here`Controller" class="com.wisdomjobs.HelloController" />

    <bean id="welcomeController" class="com.wisdomjobs.WelcomeController">
        <property name="methodNameResolver" ref="methodNameResolver" />
    </bean>

    <bean id="loginController" class="com.kruders.controller.LoginController">
        <property name="methodNameResolver" ref="methodNameResolver" />
    </bean>

    <bean id="methodNameResolver"
        class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="method" />
        <property name="defaultMethodName" value="defaultMethod" />
    </bean>


</beans>

当我尝试从控制器获取隐藏字段(selectedIds)的值时,我得到空值,当我在 jsp 的 div 标记中设置时,它正在显示我在 java 脚本中设置的 json 值。

你能帮帮我吗?

enter code here

enter image description here

【问题讨论】:

  • 对不起,我的意思是隐藏的字段值没有到达模型对象。

标签: jquery spring jsp


【解决方案1】:

要以表单形式发送参数,您必须设置名称属性。在你的selectedIds:

    <input type="hidden" id="selectedIds" name="selectedIds" value="" />

【讨论】:

    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    相关资源
    最近更新 更多