s2h整合,从数据库读数据通过jsp显示,总是出错!!

时间:2022-09-24 17:30:21
从student表取出数据,HibernateUtil类已经拿到SessionFactory
	public static List<Student> list() {
List<Student> students = new ArrayList<Student>();
String hql = "from Student";

Session session = HibernateUtil.getSession();
session.beginTransaction();
Query query = session.createQuery(hql);
students = (List<Student>)query.list();
session.getTransaction().commit();
return students;
}



StudentAction代码
public class StudentAction extends ActionSupport {
private Student student;
private List<Student> students;
private int id;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

        public List<Student> list() {
students = DBStudent.list();
return students;
}

public String add() {
DBStudent.save(student);
return SUCCESS;
}

public String loadStudent() {
DBStudent.getStudentById(id);
return SUCCESS;
}

public String update() {
Student s = DBStudent.getStudentById(id);
s = student;
DBStudent.update(s);
return SUCCESS;
}

public String delete() {
Student s = DBStudent.getStudentById(id);
DBStudent.delete(s);
return SUCCESS;
}

public String addInput() {
return INPUT;
}

public String updateInput() {
return INPUT;
}


struts.xml配置文件
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="*_Student" class="com.iman.StudentInfo.action.StudentAction" method="{1}">
            <result>/main.jsp</result>
            <result name="input">/add.jsp</result>
        </action>

    </package>

 

</struts>





jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
request.setCharacterEncoding("GBK");
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>主页</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="Content-Type" content="text/html;charset=GBK">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
    <h1 align="center">学生信息管理</h1>
    <table align="center" width="70%" height="40px" cellspacing="1" cellpadding="1" border="1">
     <tr>
     <th>学号</th>
     <th>姓名</th>
     <th>性别</th>
     <th>年龄</th>
     <th>班级</th>
     </tr>
     <s:iterator value="students" var="student">
     <tr>
     <td><s:property value="#student.id"/></td>
     <td><s:property value="#student.name"/></td>
     <td><s:property value="#student.sex"/></td>
     <td><s:property value="#student.age"/></td>
     <td><s:property value="#student.cnum"/></td>
     </tr>
     </s:iterator>
    </table>
    <center>
    <s:form action="list_Student" method="post">
    <s:submit  value="查看所有记录" ></s:submit>
    </s:form>
    </center>
  </body>
</html>





部署运行后,点击“查看所有记录”按钮后出现异常,看信息意思是说类型不匹配,把String类型赋给List了,这是哪里出现的问题啊?
Struts Problem Report
Struts has detected an unhandled exception: 

Messages: java.util.ArrayList cannot be cast to java.lang.String 
 
File: com/opensymphony/xwork2/DefaultActionInvocation.java 
Line number: 493 


--------------------------------------------------------------------------------

Stacktraces
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String 
    com.opensymphony.xwork2.DefaultActionInvocation.saveResult(DefaultActionInvocation.java:493)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:456)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept…………………………………………
  

4 个解决方案

#1


[i][/i]

#2


public List<Student> list() { 
        students = DBStudent.list(); 
        return students; 
    } 
list 方法中你返回的值是List类型的数值,而struts中的返回值必须为String类型,所以你这个会报错
你可以吧反法改为
public List<Student> list() { 
        students = DBStudent.list(); 
        return "success"; 
    } 
试试

#3


public String list() { 
        students = DBStudent.list(); 
        return "success"; 
    } 

#4


强制转型 出错了

#1


[i][/i]

#2


public List<Student> list() { 
        students = DBStudent.list(); 
        return students; 
    } 
list 方法中你返回的值是List类型的数值,而struts中的返回值必须为String类型,所以你这个会报错
你可以吧反法改为
public List<Student> list() { 
        students = DBStudent.list(); 
        return "success"; 
    } 
试试

#3


public String list() { 
        students = DBStudent.list(); 
        return "success"; 
    } 

#4


强制转型 出错了