java web SpringMVC后端传json数据到前端页面
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
下面的后台的代码:目的的查询数据库中的所有省的列表,然后转化为json传到前端页面
@Controller
public class DistrictController {
@Resource
private ProvinceService provinceServiceImp;
@Resource
private CityService cityServiceImp;
@Resource
private TourSpotService tourSpotServiceImp;
/**
* 获取所有省信息
* @param response
* @return
* @throws IOException
*/
@RequestMapping("/getAllProvince")
public String getAllProvince(HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=utf-8");//设置响应的编码格式,不然会出现中文乱码现象
PrintWriter out = response.getWriter();
//get
List<Province> provincelist = provinceServiceImp.findAllProvince();
JSONArray jsonArray = JSONArray.fromObject(provincelist);
out.write(jsonArray.toString());
out.flush();
out.close();
return null;
}
以下是前端jquery内容:
$.get("/visualization-tour/getAllProvince", function(result) {
var json = eval("("+result+")"); // 解析
var tt = "<option selected class='proInfo' value=1>~请选择省~</option>";
$.each(json, function(index) {
// 循环获取数据
var proId = json[index].proId;
var proName = json[index].proName;
tt += "<option value="+proId+">"+proName+"</option>" ;
});
$("#province").html(tt);
});
下面就是实际效果了:
注意事项,SpringMVC的控制器会是你的一些静态资源(js、html、css)不能加载,需要修改SpringMVC的配置文件,添加以下内容:
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/page/" mapping="/page/**" />
其中location 是指你的静态资源的路径
就到这里了,祝你顺利运行




适用于现代 C++ 的 JSON。
最近提交(Master分支:4 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 个月前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 个月前
更多推荐
所有评论(0)