![作为新手在学习SSM+Easyui过程中遇到一系列问题 作为新手在学习SSM+Easyui过程中遇到一系列问题](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
对于初学SSM来说,如果不熟悉SSM中SpringMVC对数据处理,会造成很大的困扰,
SSM中对前台页面放在WEB-INF下,对于读取外部信息,例如导入easyui的js文件。以及不能直接进行跳转。
主要是对于在前台页面easyui中,用easyui封装的form表单进行提交数据给SpringMV遇到问题以及跳转问题。
对于登录页面放在WEB-INF目录外,用easyui框架中form表单进行登录查询功能,对于提交数据设置为json格式,提交方式为post,
对于设置为get方式,会造成springMVC接受数据乱码,需要在tomcat中server.xml中修改一下参数。
springMVC接受json数据格式需要添加@ResponseBody,但这样会造成另一个问题,返回的数据会变成字符形式或者说不能进行跳转访问WEB-INF下页面,
即使在方法里直接调用另一个跳转方法,也只会返回一个字符串类型。本来想直接通过返回字符串来判断直接跳转的,但不能直接获取WEB-INF目录下信息,
所以直接判断返回信息用ajax进行后台连接跳转。
$("#userForm").form('submit',{
url : 'login',
contentType: "application/json;charse=UTF-8",
dataType : 'json',
success : function(data){
if(data){
$.messager.show({
title : "提示",
msg : data,
});
if(data.match('login success')){
$(location).attr('href', 'inner');
}
}
}, });
//登录查询
@RequestMapping(value="/login",method = RequestMethod.POST)
@ResponseBody
public String searchOne(User user, HttpSession session) throws IOException{
System.out.println(user);
User usr = userService.searchOne(user); if (null != usr && !"".equals(usr)) {
session.setAttribute("user", usr);
//inner(); //调用跳转方法不行
return "login success";
}else{ return "login error";
}
} //跳转
@RequestMapping("/inner")
public String inner(){ return "companySet";
}
对于某些数据提交给SpringMVC控制器时,新增提交json格式的对象,对象主键是int类型,但新增是主键会默认为null或者" ",与数据不匹配,
结果进去不了controller里面,所以需要把主键类型改为int的封装类即Integer,如果没有数据会自动默认为null或者" ",还可以在前台将主键信息默认为0,前提是mysql主键默认为自增,这样可以不回造成冲突。
以及一些查询的类型是int类型,也需要在controller方法中引入变量改成封装类型Integer。
@RequestMapping("/p/queryAll")
@ResponseBody
public Object queryAll(Integer page, Integer rows, String name, Integer idc, String address,
String sex, Integer minsal, Integer maxsal, Integer minage, Integer maxage, Integer demp_id ){
System.out.println(page+"/"+rows);
List<People> pList = new ArrayList<People>();
if(page==null && rows==null){page=0;rows=0;}
if(idc==null){idc=0;}
if(minsal==null && maxsal==null){minsal=0;maxsal=0;}
if(minage==null && maxage==null){minage=0;maxage=0;}
if(demp_id==null){demp_id=0;}
pList = peopleService.queryAll((page-1)*rows, rows, name, idc, address, sex, minsal,
maxsal, minage, maxage, demp_id);
//设置,查询全部数据
List<People> plist = peopleService.queryAll(0, 0, "", 0, "", "", 0,
0, 0, 0, 0);
int total = 0;
for (People people : plist) {
total++;
}
//传到前台easyui----总条数及分页数据
Map<String,Object> pMap = new HashMap<String,Object>();
pMap.put("total", total);
pMap.put("rows", pList);
return pMap;
}