要求刚进入页面,就需要请求后台,然后展示数据到jsp
$(function(){
//客户级别
var url="${pageContext.request.contextPath}/dict_findDictByCode.action";
var params={"dict_type_code":"006"};
$.post(url,params,function(data){
$(data).each(function(i,n){
//
var dict_id="${model.level.dict_id}";
if(dict_id==n.dict_id){
$("#cus_level").append("");
}else{
$("#cus_level").append("");
}
});
},"json");
}
if判断逻辑是为了数据回显,model是action类中个getModel()方法,返回的是Dict对象
$("#levelId").append("");
Customer对Dict是多对一的关系,所以我们需要修改Customer.java和Customer.hbm.xml。
为什么不修改Dict.java和Dict.hbm.xml呢?
因为我们不需要dict.getCustomers()来获取List
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
// private String cust_source;
// private String cust_industry;
// private String cust_level;
//Dict:Customer 一对多
private Dict source;
private Dict industry;
private Dict level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
private String filepath;
public class Role {
private String rname;
private Person person;
}
public class Person {
private String pname;
private Role role;
}
List list = new ArrayList();
Customer c = new Customer();
c.setCust_id(20L);
c.setCust_name("测试");
c.setCust_phone("120");
list.add(c);
list.add(c);
// 转换成json的字符串
String jsonString = JSON.toJSONString(list);
会报错
解决如下:
// 禁止循环的引用
String jsonString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
设置SerializerFeature
只是解决了循环检测的问题,当时如果a b互相持有对方的话,会造成死循环。
Person p = new Person();
p.setPname("美美");
Role r = new Role();
r.setRname("管理员");
p.setRole(r);
r.setPerson(p);
// 禁止循环的引用
String jsonString = JSON.toJSONString(r,SerializerFeature.DisableCircularReferenceDetect);
解决方法:
其中一方不进行序列化
public class Person {
private String pname;
@JSONField(serialize=false)
private Role role;
* method="post"
* enctype="multipart/form-data"
*
在Action中编写文件上传,需要定义三个属性
> 文件类型File ,属性名与表单中file的name属性名一致.
> 字符串类型String , 属性名:前段是name属性名一致 + ContentType;
> 字符串类型String , 属性名:前段是name属性名一致+FileName;
> 最后需要为上述的三个属性提供set方法。
> 可以通过FileUtils提供 copyFile 进行文件复制,将上传文件 保存到服务器端
private File upLoadFile;
private String upLoadFileContentType;
private String upLoadFileFileName;
public void setUpLoadFile(File upLoadFile) {
this.upLoadFile = upLoadFile;
}
public void setUpLoadFileContentType(String upLoadFileContentType) {
this.upLoadFileContentType = upLoadFileContentType;
}
public void setUpLoadFileFileName(String upLoadFileFileName) {
this.upLoadFileFileName = upLoadFileFileName;
}
/**
* 新增客户
* @return
*/
public String add() {
String path=ServletActionContext.getRequest().getContextPath();
int index = upLoadFileFileName.lastIndexOf(".");
String lastName = upLoadFileFileName.substring(index);
upLoadFileFileName=UUID.randomUUID().toString().replace("-", "")+lastName;
File file = new File(path+"/"+upLoadFileFileName);
try {
FileUtils.copyFile(upLoadFile, file);
customer.setFilepath(file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
customerService.add(customer);
return "toList";
}
在struts.xml中配置常量
或者放到action标签下的拦截器标签中
/jsp/customer/list.jsp
customer_findByPage.action
/jsp/error.jsp
2097152
.jpg,.txt
/jsp/customer/list.jsp
.jpg,.txt
代码:
https://gitee.com/ssh_jicheng/crm28day02