简体   繁体   中英

Array of object's index from request attribute in jsp and javascript

Is it possible to:

1

When I do:

$('#NContrato' ).val('${personList[0].NContrato}');

It works. But if I do

$('#NContrato' ).val('${personList[' + ind + '].NContrato}');
$('#NContrato' ).val('${personList[ind].NContrato}');
$('#NContrato' ).val('${personList[${ind}].NContrato}');

It doesn't and this is request.setAttribute(personList, ...) in a controller (I'm using spring MVC)

$('#NContrato' + ind)  // JQuery $() ...It works but
${'${personList[' + ind ...}  //Request ${} doesn't work concatenating string's
${personList[0].NContrato} // Works

Is there a way to do it?

2

I'd like to change newPerson to have the clone of ${personList[0]}

I tried this ...

<script type="text/javascript">
function setNewPerson(ind) {
${newPerson=personList[0]};
}
</script>

but .... it gives this error:

org.apache.jasper.JasperException: /WEB-INF/pages/t4imovelZMAguaEdpGass.jsp(153,3) PWC6038: "${newPerson=personList[0]}" contains invalid expression(s): javax.el.ELException: Error Parsing: "${newPerson=personList[0]}"

EDIT:

JSP

<c:forEach items="${personList}" var="item" varStatus="status">
  .....
<button type="button"  onclick="change(${item.id},${status.count})"></button> 
.....                       
</c:forEach>
     .....  

<form:form action="save()"  method="post" modelAttribute="newPerson" id="personId" >
....
<form:input path="NContrato" id="NContrato" />
 ....
</form>


<script type="text/javascript">
    function change(id, ind) {
        $('#NContrato' ).val('${personList[0].NContrato}'); //Works

        var ind=0;
        $('#NContrato' ).val("${'personList[' + ind + '].NContrato'}"); // <- 1 Question    
        ....

        ${newPerson=personList[0]}; // <- 2 Question    
    }
</script>

Controller (Servlet)

@RequestMapping(value="/saves*", method = RequestMethod.POST )
public @ResponseBody ModelAndView save(@ModelAttribute("newPerson") Person person,                                                              
    BindingResult errors, HttpServletRequest request) throws Exception {
    ...
    request.setAttribute("newPerson", subForm);
    ...
    request.setAttribute("personList", personManager.getPersonList())
    ....
    return new ModelAndView( "personJSP");
}

If you start coding JSP using EL without first learning the basics, the original old way of doing things, you will be forever confused. EL's ${} looks kind of like jquery's $() operator, but it is not.

The EL expression

${personList[0].NContrato}

is equivalent to

<%= personList[0].NContrato %>

or

<%   out.print( personList[0].NContrato ); %>

It runs on the server side and prints the value of personList[0].NContrato into the HTML source you are building.

The expression

 ${personList[' + ind + '].NContrato}

does nothing. Why? Because ${} runs on the server and only prints variables. There is no variable named "personList[' + ind + '].NContrato". That exact text "${personList[' + ind + '].NContrato}" will be printed into your HTML source. Hence the importance of viewing the source in the browser when troubleshooting.

As for

${newPerson=personList[0]};

you can't do an assignment like that in EL. Ask yourself if the following would make any sense at all?

 <% out.print( newPerson=personList[0] ); %>

It doesn't make sense to do an assignment inside a print command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM