SSH-Day02客戶關係管理系統

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>

代碼:
https://gitee.com/ssh_jicheng/crm28day02

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章