[英]Array of object's index from request attribute in jsp and javascript
Is it possible to: 是否有可能:
1 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) 它不是,这是控制器中的request.setAttribute(personList,...)(我正在使用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 2
I'd like to change newPerson to have the clone of ${personList[0]} 我想将newPerson更改为$ {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]}" org.apache.jasper.JasperException:/WEB-INF/pages/t4imovelZMAguaEdpGass.jsp(153,3)PWC6038:“ $ {newPerson = personList [0]}”包含无效的表达式:javax.el.ELException:错误解析:“ $ {newPerson = personList [0]}”
EDIT: 编辑:
JSP 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) 控制器(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编写JSP而不先学习基础知识(原始的旧做事方式),那么您将永远感到困惑。 EL's ${} looks kind of like jquery's $() operator, but it is not. EL的$ {}看起来类似于jquery的$()运算符,但事实并非如此。
The EL expression EL表达
${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. 它在服务器端运行,并将personList [0] .NContrato的值打印到您正在构建的HTML源代码中。
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". 没有名为“ personList ['+ ind +'] .NContrato”的变量。 That exact text "${personList[' + ind + '].NContrato}" will be printed into your HTML source. 确切的文本“ $ {personList ['+ ind +'] .NContrato}”将被打印到您的HTML源代码中。 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. 您不能像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. 在打印命令中进行分配没有任何意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.