Java Spring-如何使用服务层将数据库内容显示为JSP表中的列表?
我试图在JSP表中显示一个列表,我不确定我做错了什么。我应该怎样做才好?下面是一个代码: PatientDAOImpl:Java Spring-如何使用服务层将数据库内容显示为JSP表中的列表?,java,mysql,spring,jsp,Java,Mysql,Spring,Jsp,我试图在JSP表中显示一个列表,我不确定我做错了什么。我应该怎样做才好?下面是一个代码: PatientDAOImpl: public List<Patient> getAllpatients() throws SQLException { String query = "SELECT * FROM virtualclinic.patient"; Connection con = null; PreparedStatement ps = null;
public List<Patient> getAllpatients() throws SQLException {
String query = "SELECT * FROM virtualclinic.patient";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
con = dataSource.getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
List<Patient> patients = new ArrayList<Patient>();
Patient patient = new Patient();
while (rs.next()) {
patient.setId(rs.getString(1));
patient.setName(rs.getString(2));
patient.setLastName(rs.getString(3));
patient.setGender(rs.getString(4));
patient.setAge(rs.getString(5));
patient.setPhoneNumber(rs.getString(6));
patient.setAddress(rs.getString(7));
patient.setDisease(rs.getString(8));
patient.setCondition(rs.getString(9));
patient.setRoomType(rs.getString(10));
patient.setRoomNumber(rs.getString(11));
patient.setDate(rs.getString(12));
patients.add(patient);
}
return patients;
public List getAllpatients()引发SQLException{
String query=“从virtualclinic.patient中选择*”;
连接con=null;
PreparedStatement ps=null;
结果集rs=null;
con=dataSource.getConnection();
ps=合同准备陈述(查询);
rs=ps.executeQuery();
列出患者=新建ArrayList();
病人=新病人();
while(rs.next()){
患者。setId(rs.getString(1));
patient.setName(rs.getString(2));
patient.setLastName(rs.getString(3));
患者性别(rs.getString(4));
患者设置(rs.getString(5));
病人.设置电话号码(rs.getString(6));
patient.setAddress(rs.getString(7));
患者疾病(rs.getString(8));
患者设置条件(rs.getString(9));
患者。设置房间类型(rs.getString(10));
患者设置房间号(rs.getString(11));
患者设置日期(rs.getString(12));
患者。添加(患者);
}
返回病人;
ClinicServiceImpl:
public List<Patient> getAllpatients() throws SQLException{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
patientDAO = ctx.getBean("patientDAO", PatientDAOImpl.class);
return patientDAO.getAllpatients();
}
public List getAllpatients()引发SQLException{
ClassPathXmlApplicationContext ctx=新的ClassPathXmlApplicationContext(“clinicconfig.xml”);
patientDAO=ctx.getBean(“patientDAO”,PatientDAOImpl.class);
返回patientDAO.getAllpatients();
}
信息控制员:
@RequestMapping(value="/informations.html", method = RequestMethod.GET)
public ModelAndView infoPatient(Model model) throws SQLException{
setAppContext();
List<Patient> patients = clinicService.getAllpatients();
model.addAttribute("patients", patients);
ModelAndView inf = new ModelAndView("InformationsAboutPatient");
return inf;
}
public void setAppContext(){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
clinicService = ctx.getBean("clinicService", ClinicServiceImpl.class);
}
@RequestMapping(value=“/informations.html”,method=RequestMethod.GET)
公共模型和视图infoPatient(模型模型)引发SQLException{
setAppContext();
列出患者=临床医生服务。getAllpatients();
model.addAttribute(“患者”,患者);
ModelAndView inf=新的ModelAndView(“信息”);
返回inf;
}
public void setAppContext(){
ClassPathXmlApplicationContext ctx=新的ClassPathXmlApplicationContext(“clinicconfig.xml”);
clinicService=ctx.getBean(“clinicService”,ClinicServiceImpl.class);
}
Informations.jsp:
<table border="1">
<tr style="font-size: 13">
<td>ID</td>
<td>First name</td>
<td>Last name</td>
<td>Gender</td>
<td>Age</td>
<td>Phone number</td>
<td>Address</td>
<td>Disease</td>
<td>Condition</td>
<td>Room type</td>
<td>Room number</td>
<td>Date registration</td>
</tr>
<c:forEach var="patient" items="${patients}">
<tr style="font-size: 10">
<td>${patient.getId()}</td>
<td>${patient.getName()}</td>
<td>${patient.getLastName()}</td>
<td>${patient.getGender()}</td>
<td>${patient.getAge()}</td>
<td>${patient.getPhoneNumber()}</td>
<td>${patient.getAddress()}</td>
<td>${patient.getDisease()}</td>
<td>${patient.getCondition()}</td>
<td>${patient.getRoomType()}</td>
<td>${patient.getRoomNumber()}</td>
<td>${patient.getDate()}</td>
</tr>
</c:forEach>
</table>
身份证件
名字
姓
性别
年龄
电话号码
地址
疾病
条件
房间类型
房间号
登记日期
${patient.getId()}
${patient.getName()}
${patient.getLastName()}
${patient.getGender()}
${patient.getAge()}
${patient.getPhoneNumber()}
${patient.getAddress()}
${patient.getDisease()}
${patient.getCondition()}
${patient.getRoomType()}
${patient.getRoomNumber()}
${patient.getDate()}
clinicconfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="userDAO" class="org.damian.dao.UserDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="patientDAO" class="org.damian.dao.PatientDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="employeeDAO" class="org.damian.dao.EmployeeDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="clinicService" class="org.damian.service.ClinicServiceImpl">
<property name="userDAO" ref="userDAO" />
<property name="patientDAO" ref="patientDAO" />
<property name="employeeDAO" ref="employeeDAO" />
<property name="dataSource" ref="dataSource"/>
</bean>
<mvc:annotation-driven />
表中显示的值看起来与我在
表单中输入的值相同。您正在更改循环中相同的患者
对象。而不是获取关于不同患者的信息,您将得到列表中最后一个患者的重复条目
而不是这样做:
Patient patient = new Patient();
在while
循环上方,您应该在while
循环中执行此操作
while (rs.next()) {
/* Do Here */
Patient patient = new Patient();
patient.setId(rs.getString(1));
patient.setName(rs.getString(2));
patient.setLastName(rs.getString(3));
patient.setGender(rs.getString(4));
patient.setAge(rs.getString(5));
patient.setPhoneNumber(rs.getString(6));
patient.setAddress(rs.getString(7));
patient.setDisease(rs.getString(8));
patient.setCondition(rs.getString(9));
patient.setRoomType(rs.getString(10));
patient.setRoomNumber(rs.getString(11));
patient.setDate(rs.getString(12));
patients.add(patient);
}
从类中获取属性的正确语法不是调用getter而是属性名:${patient.getId()}
据我所知,应该是${patient.id}
@JordiCastilla(也称为hackability)两者之间没有区别;它们是等价的。@JordiCastilla,可能虽然使用get和set而不是直接访问可能是“更好”的方式:)在找到后,它的工作原理是这样的-s.a.getB()或s.a.b或s.getA().getB()将给出相同的结果。@Chieftwoils sweet to learn new:)谢谢!试着把这个放到你的JSP
@dante我们不是来猜测和搜索你所有的错误的,你必须提供一些关于堆栈跟踪和错误消息的反馈,而不是说**仍然不起作用**
,或者至少提供一个答案这只是其中一个问题您做得不正确的事情。setAppContext();
在做什么?您可以将您的XML配置添加到问题中吗?@JordiCastilla:我检查了服务层是否与dao配合良好,并检查了它是否正常,我在控制台中显示了所有元素(按system.out.prinln(患者))并显示了所有12个元素(就像数据库中的病人数量一样)@user2004685我在post中添加了xml文件
while (rs.next()) {
/* Do Here */
Patient patient = new Patient();
patient.setId(rs.getString(1));
patient.setName(rs.getString(2));
patient.setLastName(rs.getString(3));
patient.setGender(rs.getString(4));
patient.setAge(rs.getString(5));
patient.setPhoneNumber(rs.getString(6));
patient.setAddress(rs.getString(7));
patient.setDisease(rs.getString(8));
patient.setCondition(rs.getString(9));
patient.setRoomType(rs.getString(10));
patient.setRoomNumber(rs.getString(11));
patient.setDate(rs.getString(12));
patients.add(patient);
}