import java.util.List;
import java.util.Map;import org.hibernate.Session;
import org.hibernate.Transaction;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;import dao.Company;
import dao.Student;
import dao.StudentDAO;
@SuppressWarnings("unchecked")
public class StaffItemAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private List companystaff;
private Company company;
private StudentDAO studentdao;
public List getCompanystaff() {
return companystaff;
}
public void setCompanystaff(List companystaff) {
this.companystaff = companystaff;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public StudentDAO getStudentdao() {
return studentdao;
}
public void setStudentdao(StudentDAO studentdao) {
this.studentdao = studentdao;
}
public String execute() throws Exception
{
Map map=ActionContext.getContext().getSession();
Student student=(Student)map.get("student");
Company company=student.getCompany();
List students=null;
Session firstSession=studentdao.getSessionFactory().openSession();
Transaction tx=null;
Company companytemp=null;
try{
tx=firstSession.beginTransaction();
companytemp=(Company)firstSession.load(Company.class, company.getCompanyid());
students=studentdao.findByProperty("company", companytemp);
tx.commit();
}
catch(Exception e){
if(tx!=null)
tx.rollback();
throw e;
}
finally{
firstSession.close();
}
if(students.size()==0)
{
this.setCompany(companytemp);
return "success";
}
else
{
this.setCompany(companytemp);
this.setCompanystaff(students);
return "success";
}
}}
在action里用set方法设置了一个List的值,存放的是数据库查询的结果Student类型的对象
然后在页面上迭代输出companystaff的值,但是这有个问题,由于Student类型里有个属性是City类型的(hibernate:外键生成)感觉是因为实际上未加载实际对象,所以在迭代中依次输出每个Student的各个属性的时候,City这个属性不会显示,不知如何解决。我的显示代码片段为:
<s:iterator id="students" value="companystaff" status="st">
<tr >
<td colspan="2" bgcolor="#ff80ff">
<s:property value="username"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="college"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="cityByTownId.cityname"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="major"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="position"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="sex"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="phone"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="email"/>
</td>
</tr>
</s:iterator>
import java.util.Map;import org.hibernate.Session;
import org.hibernate.Transaction;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;import dao.Company;
import dao.Student;
import dao.StudentDAO;
@SuppressWarnings("unchecked")
public class StaffItemAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private List companystaff;
private Company company;
private StudentDAO studentdao;
public List getCompanystaff() {
return companystaff;
}
public void setCompanystaff(List companystaff) {
this.companystaff = companystaff;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public StudentDAO getStudentdao() {
return studentdao;
}
public void setStudentdao(StudentDAO studentdao) {
this.studentdao = studentdao;
}
public String execute() throws Exception
{
Map map=ActionContext.getContext().getSession();
Student student=(Student)map.get("student");
Company company=student.getCompany();
List students=null;
Session firstSession=studentdao.getSessionFactory().openSession();
Transaction tx=null;
Company companytemp=null;
try{
tx=firstSession.beginTransaction();
companytemp=(Company)firstSession.load(Company.class, company.getCompanyid());
students=studentdao.findByProperty("company", companytemp);
tx.commit();
}
catch(Exception e){
if(tx!=null)
tx.rollback();
throw e;
}
finally{
firstSession.close();
}
if(students.size()==0)
{
this.setCompany(companytemp);
return "success";
}
else
{
this.setCompany(companytemp);
this.setCompanystaff(students);
return "success";
}
}}
在action里用set方法设置了一个List的值,存放的是数据库查询的结果Student类型的对象
然后在页面上迭代输出companystaff的值,但是这有个问题,由于Student类型里有个属性是City类型的(hibernate:外键生成)感觉是因为实际上未加载实际对象,所以在迭代中依次输出每个Student的各个属性的时候,City这个属性不会显示,不知如何解决。我的显示代码片段为:
<s:iterator id="students" value="companystaff" status="st">
<tr >
<td colspan="2" bgcolor="#ff80ff">
<s:property value="username"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="college"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="cityByTownId.cityname"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="major"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="position"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="sex"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="phone"/>
</td>
<td colspan="2" bgcolor="#ff80ff">
<s:property value="email"/>
</td>
</tr>
</s:iterator>
解决方案 »
- 请教:dwr的batch和hooks
- s2sh如何通过配置文件给jsp中的select赋值
- windows+tomcat jsp正常; linux+tomcat jsp中文乱码 如何 解决?
- 大家帮帮我呀.如何写,比如:要限制用户名的字段的长度
- struts2.0拦截器问题
- 关于jive中的问题
- 在线等待:急急!!怎样做“拖动网页的滚动条,网页上的图标也跟着动”???给分!!
- 在JSP中如何输出特殊字符?
- 关于学习JSP,SERVLET要点经验
- JSP中没有任何错误,但出现下面的异常可能是程序哪一块出现了问题?(在线等待)
- jsp+Access,使用group by 后,提示游标无效
- 麻烦帮忙解决一下
1.对象定义
public class TrnRelation implements java.io.Serializable {
private Integer relationId;
private TrnAddressRecord trnAddressRecord; //对象属性
}2.取值
public TrnRelation findByRelationId(Integer relationId) {
TrnRelation trnRelation = trnRelationDAO.findByRelationId(relationId);
if (null != trnRelation) {
if (trnRelation.getTrnAddressRecord() != null) {
trnRelation.getTrnAddressRecord().getSyncKbnCd();
}
}
return trnRelation;
}
1.对象定义
public class TrnRelation implements java.io.Serializable {
private Integer relationId;
private TrnAddressRecord trnAddressRecord; //对象属性
} 2.取值
public TrnRelation findByRelationId(Integer relationId) {
TrnRelation trnRelation = trnRelationDAO.findByRelationId(relationId);
if (null != trnRelation) {
…