如何从java类获取可变数据到javascript?

时间:2016-05-18 04:33:11

标签: javascript java jsp

public String getVatNetAmount()
    {
        String expensetype = getRequest().getParameter("type");
        String localAmount = getRequest().getParameter("amount");
        Double localamt = Double.parseDouble(localAmount); 

        Double netAmount;


        netAmount = getExpensesTypeManager().getVatNetAmount(expensetype,localAmount);

         am = (float) ((localamt * netAmount)/100);
        // getRequest().setAttribute("NetAmount", am);

        return INPUT;
    }

这是我的java类方法,我想在jsp页面或javascript函数中获取变量am中的数据?

2 个答案:

答案 0 :(得分:0)

您可以这样使用:

<%
    String var="sth";
%>

<script>
     var value = "<%=var%>";
</script>

答案 1 :(得分:0)

在Spring MVC中,您可以使用

将模型添加到jsp
ModelAndView mv = new ModelAndView();
mv.addObject("keyForJsp",object); // you can get data in jsp by using the key "keyForJsp"

现在用javascript获取它。 (使用JSTL lib)

<script>
    var data = '${keyForJsp}';
</script>