如何在javascript中获取Java布尔值?

时间:2016-11-02 16:02:43

标签: javascript java spring

我想从jsp:

进行ajax调用
<script type="text/javascript">

    function isTablePkUsedInFk(table_, pkVal) {

        var url_ = "<c:url value='/ajaxIsTablePkUsedInFk' />";

        var ret = $.ajax({
            data : {"table" : table_ , "pkVal" : pkVal},
            type : "POST",
            url  : url_,
            async: false
        }).responseText;

        ret = $.trim(ret);

        return ret;

    }

</script>

在控制器中:

@Controller
@RequestMapping("/")
public class Home {

    @Autowired
    private Environment env;

    @Autowired
    private UtilisateurDAO utilisateurDao;

    @Autowired
    private MenuDAO menuDao;

    @Autowired
    private DbDAO dbDao;

    @RequestMapping(value = "/ajaxIsTablePkUsedInFk", method = RequestMethod.POST)
    @ResponseBody
    public boolean ajaxIsTablePkUsedInFk(HttpServletRequest request) {
        String table = request.getParameter("table");
        String pkVal = request.getParameter("pkVal");
        return dbDao.isTablePkUsedInFk(table, pkVal);
    }

}

在运行时,当我调用javascript函数isTablePkUsedInFk时没有任何反应!那么如何在javascript中获取Java布尔值?

2 个答案:

答案 0 :(得分:5)

您无法从其他控制器返回原语。 Controller可以返回表示此布尔值的String对象。

@ResponseBody
public String ajaxIsTablePkUsedInFk(HttpServletRequest request) {
  ...
  return Boolean.toString(dbDao.isTablePkUsedInFk(table, pkVal));
}

答案 1 :(得分:0)

尝试使用response.tablePkUsedInFk代替response.isTablePkUsedInFk