在javascript中获取requestattributes

时间:2013-02-06 04:28:28

标签: javascript jstl

我想在javascript中检索请求范围属性,如下所示。我怎样才能做到这一点?

function caseChanges(req) {
    var innervalue= "${dashboardTicketSummary}"
    alert("nand you are 1234");
    alert(innervalue);
} 

2 个答案:

答案 0 :(得分:0)

在jsp页面上确认javascript是内联的 - 你在正确的轨道上,你只需要确保在HttpServletRequest对象上设置属性(通常由你的控制器类),即:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
{
  DashboardTicketSummary dts = ...; // code to obtain ticket summary here

  req.setAttribute("dashboardTicketSummary", dts);

  // code to dispatch to your jsp page here
}

当然还有许多其他更复杂的方法 - 特别是你使用流行的MVC框架,如Spring

答案 1 :(得分:0)

设置值以请求属性并在jsp页面上的脚本标记中写入这段代码(如index.jsp):

<script>
    function caseChanges(req){
        var innervalue= "${dashboardTicketSummary}"
        alert("nand you are 1234");
        alert(innervalue);
    } 
</script>

为了更好的可维护性,您可以将所有属性收集到jsp上的全局对象,并在其他js文件上引用它:

的index.jsp

<script>
    var REQUEST_ATTRS = {
            dashboardTicketSummary : '${dashboardTicketSummary}',
            otherAttributes :'${otherAttributes}'
        };
</script>

other.js

function caseChanges(req){
    var innervalue= REQUEST_ATTRS.dashboardTicketSummary ;
    alert("nand you are 1234");
    alert(innervalue);
}