《在Eclipse下实践Struts+Hibernate 一》只是对数据库进行插入操作,现在来个从数据库中查询并显示,
在Action类中添加getName()引用,CommonExample.java全文如下:
package com.yeepal.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
public class CommonExample extends ActionSupport {
private static final long serialVersionUID = 1L;
Configuration cfg = new Configuration().configure();
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
public String rname;
public String execute() throws Exception {
customer.setName("phengchen");
customer.setEmail("utyphoon@126.com");
customer.setPassword("12345678");
rname = customer.getName();
session.save(customer);
tx.commit();
session.close();
return SUCCESS;
}
}
注意看中间两行黑体字部分,先定义一个变量,然后调用getName方法,就可以从数据表里得到一个查询结果,虽然方法是粗旷了点,不过结果还是成功的。
然后为了显示,我们需要做一个JSP页面,在做页面之前,我们还是需要修改一下struts.xml的,修改后的全文如下:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="default" extends="struts-default">
<action name="hello"
class="com.yeepal.test.HelloAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="commonexample"
class="com.yeepal.test.CommonExample">
<result name="success">s2.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
注意看黑体字那行,也就是说如果action返回一个success的话,就调用s2.jsp页面,然后s2.jsp页面的内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:property value="rname" /> <br>
</body>
</html>
也是个超级简单的页面,就显示一个查询出来的值,注意看黑体字部分。
运行一下看看吧。
分享到:
相关推荐
《JavaWeb整合开发完全自学手册》介绍如何整合Struts+Hibernate+Spring+Eclipse进行J2EE开发,所有实例都基于MyEclipseIDE开发,引领读者快速进入基于JavaWeb的J2EE应用领域。《JavaWeb整合开发完全自学手册》主要...
精通J2EE——Eclipse+Struts+Hibernate+Spring整合应用案例 pdf电子书, 包含所有18个章节的完整版, 美中不足的是扫描质量不是很高,且是第一版的,不是目前最新的第二版! 请看清楚了再下,免得后悔! 共有两个部分-这...
整合Eclipse struts+hibernate+spring2
一个spring+struts+hibernate的例子,是eclipse的工程,用tomcat5和mysql,文件夹下包含所有的源码和库,另外还有一个.sql的文件用于建立数据库。大家觉得它有什么不好,欢迎交流
Eclipse+Struts+Spring+Hibernate+Mysql环境搭建及访问数据库实例
基于Struts2的SSH2整合架构,并在此架构基础上实现一个完整的办公自动化系统。在《开发者突击:Java Web主流框架整合开发(J2EE+Struts+Hibernate+Spring)》架构讲解的过程中,穿插介绍了JDBC、Log4j、Sitemesh、JUnit...
eclipse(struts2+spring3+hibernate3)案例附带mysql数据库
eclipse中整合spring+struts+hibernate,能直接运行
struts2+hibernate的整合
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
Struts+Hibernate案例(实现增删查改)适合初学者 Struts+Hibernate案例(实现增删查改)适合初学者
精通J2EE-Eclipse+Struts+Hibernate+Spring整和应用案例
精通J2EE-Eclipse+Struts+Hibernate+Spring整和应用案例
eclipse_3.4_三大框架(struts+hibernate+spring)搭建
Java项目之图书管理系统源代码,使用了struts+hibernate+spring等技术,需要的小伙伴不要错过了。Java项目之图书管理系统源代码,使用了struts+hibernate+spring等技术,需要的小伙伴不要错过了。
java开发Eclipse+struts+Hibernate制作图形验证码及登录视频
2.加载 hibernate-distribution-3.3.2.GA-dist.tar.gz,连数据库,看正常插入。 3.删除Hibernate的配置文件Hibernate.cfg.xml,工厂类HibernateSesseionFactory类,交由spring去做。 注意: 数据库:mysql
用struts+hibernate做的两个文件上传的例子,当然您也可以自己修改成多个文件上传!
struts+hibernate简单的学生管理信息系统的完整源码 适合初学者
Eclipse+Struts+Spring+Hibernate+Mysql环境搭建及访问数据库实例.doc