我的一个要求是在我的setter方法中获取session属性,以便在设置数据时我可以根据我的要求使用我的数据并按预期返回新数据。
是否可以在我的模型类中获取HTTP会话对象,如果没有,是否有任何其他方法可以实现此目的。
这是我的模型类。
DiscoveredProduct
package com.hcl.ne.model;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
public class DiscoveredProduct {
private String _id;
private String _rev;
private String name;
private String productCatalogName;
private String normalizedProductName;
private String domain;
private String desc;
private String product;
private String company;
private String dataset;
private String classType;
private Map<String,Object> attributes[];
private List<Map<String,Object>> attributes2;
private String hostName;
private String serialNumber;
private String createdOn;
private String qulRuleId;
private String classId;
private String companyId;
private String selectedDiscoveredPdt;
private int pageSize;
private String isNormalized;
private String ne_status;
private Map<String,Object> normalizedAttributes[];
private String normalizedOn;
public String getNe_status() {
return ne_status;
}
public void setNe_status(String ne_status) {
this.ne_status = ne_status;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String get_rev() {
return _rev;
}
public void set_rev(String _rev) {
this._rev = _rev;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDataset() {
return dataset;
}
public void setDataset(String dataset) {
this.dataset = dataset;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public String getProductCatalogName() {
return productCatalogName;
}
public void setProductCatalogName(String productCatalogName) {
this.productCatalogName = productCatalogName;
}
public Map<String, Object>[] getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object>[] attributes) {
this.attributes = attributes;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getCreatedOn() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
HttpSession session = new
this.createdOn = createdOn;
}
public List<Map<String, Object>> getAttributes2() {
return attributes2;
}
public void setAttributes2(List<Map<String, Object>> attributes2) {
this.attributes2 = attributes2;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getCompanyId() {
return companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
public String getSelectedDiscoveredPdt() {
return selectedDiscoveredPdt;
}
public void setSelectedDiscoveredPdt(String selectedDiscoveredPdt) {
this.selectedDiscoveredPdt = selectedDiscoveredPdt;
}
public String getQulRuleId() {
return qulRuleId;
}
public void setQulRuleId(String qulRuleId) {
this.qulRuleId = qulRuleId;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getIsNormalized() {
return isNormalized;
}
public void setIsNormalized(String isNormalized) {
this.isNormalized = isNormalized;
}
public String getNormalizedProductName() {
return normalizedProductName;
}
public void setNormalizedProductName(String normalizedProductName) {
this.normalizedProductName = normalizedProductName;
}
public Map<String, Object>[] getNormalizedAttributes() {
return normalizedAttributes;
}
public void setNormalizedAttributes(Map<String, Object>[] normalizedAttributes) {
this.normalizedAttributes = normalizedAttributes;
}
public String getNormalizedOn() {
return normalizedOn;
}
public void setNormalizedOn(String normalizedOn) {
this.normalizedOn = normalizedOn;
}
}
已编辑的代码
package com.hcl.ne.model;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import com.hcl.ne.service.util.CommonUtility;
public class DiscoveredProduct {
private String _id;
private String _rev;
private String name;
private String productCatalogName;
private String normalizedProductName;
private String domain;
private String desc;
private String product;
private String company;
private String dataset;
private String classType;
private Map<String,Object> attributes[];
private List<Map<String,Object>> attributes2;
private String hostName;
private String serialNumber;
private String createdOn;
private String qulRuleId;
private String classId;
private String companyId;
private String selectedDiscoveredPdt;
private int pageSize;
private String isNormalized;
private String ne_status;
private Map<String,Object> normalizedAttributes[];
private String normalizedOn;
@Autowired
private HttpSession httpSession;
public String getNe_status() {
return ne_status;
}
public void setNe_status(String ne_status) {
this.ne_status = ne_status;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String get_rev() {
return _rev;
}
public void set_rev(String _rev) {
this._rev = _rev;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDataset() {
return dataset;
}
public void setDataset(String dataset) {
this.dataset = dataset;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public String getProductCatalogName() {
return productCatalogName;
}
public void setProductCatalogName(String productCatalogName) {
this.productCatalogName = productCatalogName;
}
public Map<String, Object>[] getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object>[] attributes) {
this.attributes = attributes;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getCreatedOn() {
long epochDate = Long.parseLong(createdOn);
System.out.println(epochDate);
HttpSession session = getHttpSession();
String timeZone = (String) session.getAttribute("timeZone");
String dateFormat = (String) session.getAttribute("dateFormat");
String offSet = (String) session.getAttribute("offSetValue");
String resultDate = CommonUtility.getDateInGMT(epochDate, timeZone, dateFormat, offSet);
return resultDate;
}
public void setCreatedOn(String createdOn) {
this.createdOn = createdOn;
}
public List<Map<String, Object>> getAttributes2() {
return attributes2;
}
public void setAttributes2(List<Map<String, Object>> attributes2) {
this.attributes2 = attributes2;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getCompanyId() {
return companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
public String getSelectedDiscoveredPdt() {
return selectedDiscoveredPdt;
}
public void setSelectedDiscoveredPdt(String selectedDiscoveredPdt) {
this.selectedDiscoveredPdt = selectedDiscoveredPdt;
}
public String getQulRuleId() {
return qulRuleId;
}
public void setQulRuleId(String qulRuleId) {
this.qulRuleId = qulRuleId;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getIsNormalized() {
return isNormalized;
}
public void setIsNormalized(String isNormalized) {
this.isNormalized = isNormalized;
}
public String getNormalizedProductName() {
return normalizedProductName;
}
public void setNormalizedProductName(String normalizedProductName) {
this.normalizedProductName = normalizedProductName;
}
public Map<String, Object>[] getNormalizedAttributes() {
return normalizedAttributes;
}
public void setNormalizedAttributes(Map<String, Object>[] normalizedAttributes) {
this.normalizedAttributes = normalizedAttributes;
}
public String getNormalizedOn() {
return normalizedOn;
}
public void setNormalizedOn(String normalizedOn) {
this.normalizedOn = normalizedOn;
}
public HttpSession getHttpSession() {
return httpSession;
}
public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
}
答案 0 :(得分:1)
是的,这是可能的。
- 使用RequestContextHolder
- 通过自动装配会话。
醇>
1的 RequestContextHolder
强>
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession();
2的 AutoWired
强>
@Autowired
private HttpSession httpSession;
更新:无需使用getter和setter,自动装配并使用httpSession.getAttribute()