select
要求刚进入页面,就需要请求后台,然后展示数据到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){
//<option selected="selected"></option>
var dict_id="${model.level.dict_id}";
if(dict_id==n.dict_id){
$("#cus_level").append("<option selected='selected' value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
}else{
$("#cus_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
}
});
},"json");
}
if判断逻辑是为了数据回显,model是action类中个getModel()方法,返回的是Dict对象
$("#levelId").append("<option value='"+n.dict_id+"' selected>"+n.dict_item_name+"</option>");
多对一
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;
<hibernate-mapping>
<class name="com.cqc.crm.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="user_password"/>
<!-- <property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/> -->
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
<property name="filepath" column="filepath"/>
<!-- 多对一 -->
<many-to-one name="source" class="com.cqc.crm.domain.Dict" column="cust_source"/>
<many-to-one name="industry" class="com.cqc.crm.domain.Dict" column="cust_industry"/>
<many-to-one name="level" class="com.cqc.crm.domain.Dict" column="cust_level"/>
</class>
</hibernate-mapping>
fastjson
public class Role {
private String rname;
private Person person;
}
public class Person {
private String pname;
private Role role;
}
问题一:fastjson的循环引用
List<Customer> list = new ArrayList<Customer>();
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);
问题二:fastjson的死循环
设置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;
上传File
jsp页面要求
* method="post"
* enctype="multipart/form-data"
* <input type="file" name="myfile">
action要求
在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中配置常量
<struts>
<!-- 设置上传文件的总大小,默认是2M struts.multipart.maxSize=2097152 -->
<constant name="struts.multipart.maxSize" value="20971520"/>
</struts>
或者放到action标签下的拦截器标签中
<action name="customer_*" class="customerAction" method="{1}">
<result name="list">/jsp/customer/list.jsp</result>
<result name="toList" type="redirectAction">customer_findByPage.action</result>
<result name="input" type="redirectAction">/jsp/error.jsp</result>
<!-- 引入默认的拦截器 -->
<interceptor-ref name="defaultStack">
<!-- 设置单个上传文件的大小 -->
<param name="fileUpload.maximumSize">2097152</param>
<!-- 决定上传文件的类型 -->
<param name="fileUpload.allowedExtensions">.jpg,.txt</param>
</interceptor-ref>
</action>
怎么限制上传文件的后缀名?
<action name="customer_*" class="customerAction" method="{1}">
<result name="list">/jsp/customer/list.jsp</result>
<!-- 引入默认的拦截器 -->
<interceptor-ref name="defaultStack">
<!-- 决定上传文件的类型 -->
<param name="fileUpload.allowedExtensions">.jpg,.txt</param>
</interceptor-ref>
</action>