ssh整合struts 单元测试报错 初学大家帮帮忙

tanggoodboy 2007-07-01 11:53:46
servletunit.struts.ExceptionDuringTestError: An uncaught exception was thrown during actionExecute()
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:305)
at com.software.web.action.IndexActionTest.testVeriry(IndexActionTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
------------
Root Cause:
------------
javax.servlet.UnavailableException: Cannot initialize RequestProcessor of class org.springframework.web.struts.DelegationRequestProcessor: java.lang.ClassNotFoundException: org.springframework.web.struts.DelegationRequestProcessor
at org.apache.struts.action.ActionServlet.getRequestProcessor(ActionServlet.java:595)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1192)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:290)
at com.software.web.action.IndexActionTest.testVeriry(IndexActionTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

测试类为:
package com.software.web.action;

import java.io.File;

import servletunit.struts.MockStrutsTestCase;

import com.software.web.core.SessionContainer;

public class IndexActionTest extends MockStrutsTestCase {

@Override
protected void setUp() throws Exception {
// TODO 自动生成方法存根
super.setUp();
//加上这个路径 否则会说扎不到web.xml文件的
setContextDirectory(new File("WebRoot"));
}

@Override
protected void tearDown() throws Exception {
// TODO 自动生成方法存根
super.tearDown();
}
//测试方法,这里看源码
public void testVeriry()
{
this.addRequestParameter("username", "tang");
this.addRequestParameter("password", "429");
this.addRequestParameter("state", "verify");
//自动设置url
this.setRequestPathInfo("/index");
//进行测试
/*
* Executes the Action instance to be tested. This method calls the ActionServlet.doPost() method to execute
* the Action instance to be tested
*/
this.actionPerform();
//没有错误
this.verifyNoActionErrors();
//main文件夹下
this.verifyForward("main");
//进行断言
String user = ((SessionContainer)request.getSession().getAttribute("SessionContainer")).getUser().getUsername();
this.assertEquals("tang",user);
}

}

测试出现以上异常,大家帮帮忙
...全文
479 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanggoodboy 2008-08-07
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.custom.i18n.resources" value="message"></constant>

<package name="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>

<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>

<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">

</interceptor>

<interceptor-stack name="myStack">

<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>

-->

<interceptor-ref name="defaultStack"></interceptor-ref>

</interceptor-stack>
</interceptors>

<default-interceptor-ref name="myStack"></default-interceptor-ref>

<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/result.jsp</result>
<result name="failer">/login2.jsp</result>
</action>

<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
<result name="input">/input.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction" method="test">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>

<interceptor-ref name="myInterceptor3">
<param name="excludeMethods">test,execute</param>
<param name="includeMethods">test</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

</action>

</package>

</struts>
tanggoodboy 2008-08-07
  • 打赏
  • 举报
回复
1. 真正存放field级别错误信息的对象是LinkedHashMap

2. 该LinkedHashMap的key是String类型的,value是ArrayList类型的

3. 对于Action级别的错误信息,实际上是放置在ArrayList中的

4 拦截器:Interceptor

拦截器就是一个类:

目标对象:
拦截器对象
对目标对象生成的代理对象,执行的是代理对象:系统产生,不是自己

写出来,和目标对象的方法类似,但是是综合了拦截器对象的方法和目

标对象的方法

处理器: 真正调用目标对像方法,加入拦截器的操作(方法前后)。
代理对象: 将传过来的目标对象,加入拦截器,返回一个代理对象,

返回代理对象对目标对象的方法调用。


过滤器练:
拦截器站:
ActionInvocation invocation的invoke判断是否还有拦截器,如果有

的话就执行拦截器;


在struts.xml中配置拦截器 :
<interceptor name="" class=" "></interceptor>

如果哪个action使用就用哪个action
在action中result下配置<interceptor-ref

<interceptor-ref name="myInterceptor3">
<param

name="excludeMethods">test,execute</param>
<param

name="includeMethods">test</param>
</interceptor-ref>
>
默认拦截器只有一个 ,默认被调用,如果action中手工引用拦截器,

那么就不会调用,只有手工完成了
如果:自己手写了拦截器,那么自己应该手动加载原来的默认的拦截器

tanggoodboy 2008-08-04
  • 打赏
  • 举报
回复
Hibernate问题小结2008-06-20 01:243.1对多对一的查询
HQL语句会映射成SQL语句,将role映射成role 的id,所以下面的语句可以执行;

RolePopedom(多)——Role(一),可以理解role对象在Hibernate里对应roleid,所以下面的语句可以执行;
public boolean deleteRolePopedomByRoleId(Integer roleid) {
// TODO Auto-generated method stub
String sql = "delete RolePopedom where role = "+roleid;
try {
HibernateSession.executeSQL(this.hibernateTemplate,sql);
return true;
}catch(DataAccessException e) {
e.printStackTrace();
return false;
}
}
3.2在HibernateTemplate里执行Sql语句
如下所示只能执行非Select语句;
public static void executeSQL(HibernateTemplate hibernateTemplate,
String sql)
{
final String tempsql = sql;
hibernateTemplate.execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
throws HibernateException
{
session.createQuery(tempsql).executeUpdate();
return null;
}
});
}
若要执行Select则用session.createQuery(tempsql).list();
此处final String tempsql = sql;必须为final 表示tempsql不可以修改;
内联类的要求;
public Object doInHibernate(Session session)返回Object等于hibernateTemplate.execute(new HibernateCallback()的返回,

可以通过查看源代码看的出来;
下面是执行select语句的情况:
return (List)hibernateTemplate.execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
throws HibernateException
{
List list=session.createQuery(tempsql).list();
return list;
}
});
3.3多对一映射
<hibernate-mapping>
<class name="cn..popedom.hibernate.RolePopedom" table="rolepopedom">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<many-to-one name="role"
column="role_id"
class="cn..role.hibernate.Role"
cascade="save-update"
lazy="false"
not-null="true">
</many-to-one>
<many-to-one name="popedom"
column="popedom_id"
class="cn..popedom.hibernate.Popedom"
cascade="save-update"
not-null="true">
</many-to-one>
</class>
</hibernate-mapping>
cascade="save-update"级联保存和更新,还有级联删除
3.4小错误问题
3.4.1多删除一次的问题
0 actual row count: 0 expected: 1
可能是多删除一次的问题
3.4.2 hql中的赋值
1. "delete User where id='"+userid[i]+"'";
字符串赋值需要加单引号,整型不需要。
3.4.2 hql中的赋值
1."delete User where id='"+userid[i]+"'";
用这个方法删除对象不会级联删除。用传入对象的方法删除可以级联删除
3.5中文查询问题
Hibernate对中文查询有Bug,所以不能直接写在HQL语句中,比如说:
from cat where cat.name like ‘你好’
这样是可行的:
String hql="from table as t where t.name like ?and t.address like ? and t.title=?";
String[] parameter={new String(“%”+myName+”%”), new String(“%”+myAddress+”%”), new String(“%”+myTitle+”%

”)};
this.getHibernateTemplate().find(hql,paramter);

3.6 DAO操作问题
Hibernate的破坏性操作都应使用事务处理;对非破坏性操作可以放宽要求;

kotty1 2008-07-30
  • 打赏
  • 举报
回复
参数说明:
id : Number Unique identity number.
(此节点的序列数字)
pid : Number Number refering to the parent node. The value for the root node has to be -1.
(此节点的父节点序列数字,如果此节点为根节点,那么父节点序列值必须为-1)
name : String Text label for the node.
(节点名称)
url : String Url for the node.
(节点的连接地址)
title : String Title for the node.
(鼠标移动到上面时,显示的内容)
target : String Target for the node.
(节点连接地址显示的位置,如果为空的话,则上面的连接地址会在节点所在的这个窗口打开!)
icon : String Image file to use as the icon. Uses default if not specified.
(节点关闭时的图标)
iconOpen : String Image file to use as the open icon. Uses default if not specified.
(节点打开时的图标)
open : Boolean Is the node open.
example: (不晓得什么参数)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>Destroydrop » Javascripts » Tree</title>

<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>

</head>

<body>

<h1><a href="/">Destroydrop</a> » <a href="/javascripts/">Javascripts</a> » <a href="/javascripts/tree/">Tree</a></h1>

<h2>Example</h2>

<div class="dtree">

<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>

<script type="text/javascript">
<!--

d = new dTree('d');

d.add(0,-1,'My example tree');
d.add(1,0,'Node 1','example01.html');
d.add(2,0,'Node 2','example01.html');
d.add(3,1,'Node 1.1','example01.html');
d.add(4,0,'Node 3','example01.html');
d.add(5,3,'Node 1.1.1','example01.html');
d.add(6,5,'Node 1.1.1.1','example01.html');
d.add(7,0,'Node 4','example01.html');
d.add(8,1,'Node 1.2','example01.html');
d.add(9,0,'My Pictures','example01.html','Pictures I\'ve taken over the years','','','img/imgfolder.gif');
d.add(10,9,'The trip to Iceland','example01.html','Pictures of Gullfoss and Geysir');
d.add(11,9,'Mom\'s birthday','example01.html');
d.add(12,0,'Recycle Bin','example01.html','','','img/trash.gif');

document.write(d);

//-->
</script>

</div>

<p><a href="mailto:drop@destroydrop.com">©2002-2003 Geir Landrö</a></p>

</body>

</html>
kotty1 2008-07-30
  • 打赏
  • 举报
回复
初学Ant.

日期:2006-8-5 20:08:00 [Host01.Com] 访问:     [全屏查看全文]
工作需要,开始接触java,根据N年的C++经验,学习一门语言不外乎3件事,语法,编译部署工具,library。 java语法与C++和.Net差异不大,OO的概念也没有太大的不同;一些特殊的语法点,比如final,GC,reflect到处都是相关讨论。鉴于此,我将学习的重点放在公用库的使用和编译部署工具的学习上。
这是一个系列blog,先是一些java相关的编译,配置工具,然后是一些常用的java开源库,最后是eclipse。eclipse会是最终的落脚点,再次之前,本人需要一些java基础,现在,就从ant开始。


1. 下载,环境配置:

应该说,ant的文档对比大部分c++工具和库来说,是非常优秀的。从apache上下载Ant到编译,跑起来,不过花费了20分钟。当然,我知道一般使用binary就行了,但是既然Ant是一个build工具,观察一下它自己的编译过程总是有点意思的。 下载Ant,位置是http://ant.apache.org/,解压缩到d盘,目录结构如下:
d:/apache-ant-1.6.2/
bin/
docs/
etc/
lib
src/
一般关注bin/ant.bat就行了(windows平台),有兴趣的话可以分析一下ant.bat文件。
我注意到几行脚本:
:runAnt
if not "%CLASSPATH%"=="" goto runAntWithClasspath
"%_JAVACMD%" %ANT_OPTS% -classpath "%ANT_HOME%\lib\ant-launcher.jar" "-Dant.home=%ANT_HOME%" org.apache.tools.ant.launch.Launcher %ANT_ARGS% %ANT_CMD_LINE_ARGS%
goto end
显然,org.apache.tools.ant.launch.Launcher就是Ant的main函数文件,搜索一下src目录,果然在文件Launcher.java中找到了main函数。


2. 查看文档,制作配置文件:

核心是编写一个build.xml文件,也就是C++中的Makefile文件,只不过,个人感觉build.xml文件的编写比Makefile要简单不少,因为绝大部分工作Ant都已经做好了。废话少说,以下是我测试用的一个build.xml文件。我的测试工程目录结果如下:
d:/test/
src/
doc/
lib/

源代码都放在src目录下,改目录又有4个子目录,Cmd,Msg,Plugin,Ant本身内置目录的递给搜索,所以可以完全不考虑子目录的存在,当然,如果需要精细控制编译过程,如使用了lex之类的工具自动生成代码等,另当别论,这里只考虑最简单的情况。我手动书写的build.xml如下:
<project name="testAnt" basedir="." default="run">
<target name="init">
<mkdir dir="build">
</target>

<target name="compile" depend="init">
<javac srcdir="src" destdir="build">
<jar jarfile="test.jar" basedir="build"/>
</target>

<target name="run" depend="compile">
<java classname="test.ClassMain" jar="test.jar"/>
</target>

<target name="clear">
<delete dir="build"/>
<delete file="test.jar"/>
</target>
</project>

3. 分析这个简单的build.xml文件:

这个配置文件是一个project节点,3个属性,name表示porject的名字,basedir表示内部相对路径的起点,default表示ant默认的开始执行的target,对于上面的build.xml,如果用户在当前目录敲入ant,那么Ant首先找到"run"所对应的target,也就是第三个target,然后ant发现这个target依赖(depend)与"compile"所对应的target,那么把"run"入栈,先执行""compile"任务。 然后ant会发现"compile"又依赖于"init"任务,所以先将"compile"入栈,执行"init"。
"init"只是在项目根目录下建议一个目录。执行完"init"后,"compile"出栈,被执行。
"compile"分两步执行,首先,编译"src"目录下的所有.java文件(递给搜索),将生成的.class文件放入"build"目录(如果有递归子目录,会自动生成与原目录相同的目录层次).编译完成后,把build目录打包成一个jar文件。
"cimpile"执行完成,开始执行"run",实际上就是调用java.exe运行编译出来的项目。

当然,如果您需要清除以前的编译结果,可以简单输入ant clear。实际执行过程是"clear"对应的 target被执行,删除"build"目录和test.jar文件。


以上是本人开始学习ant的一个过程,总体感觉ant确实非常好使。当然,对于复杂的工程来说,配置文件的书写可能也会相对复杂,但是毕竟
比手工维护Makefile或者使用Automake管理Makefile来说,要轻松的太多了。
kotty1 2008-07-30
  • 打赏
  • 举报
回复
Ant在WebLogic Service 的目录bea/weblogic81/serve/bin中存在
要使用Ant设置环境变量
Set PATH=c:/bea/weblogic81/server/bin;%PATH%
Ant.bat

指定其它bulid文件而不使用bulid.xml文件的示例:
Ant.bat-bulidfile MyBulidFile.xml
Ant.bat-bulidfile/demo/BulidApplication.xml

Javac
<javac crcdir=”myproject/mysource” destdir=”myproject/classes” excludes=”mypackage/**”
Classpath=”mylibrary.jar”/>
**代表所有的当前的目录,excludes为了包含进来其他一些文件
Java
<java classname=”test.Mytester”>
<classpath>
<pathelement path=”myproject/classes”/>
<pathelement location=”Mylibrary.jar”/>
</classpath>
</java>

Jar
<jar jarfile=”myArchive.jar” basedir=”myproject/root”
Excludes=”myArchive.jar” update=”yes”/>

War
<war warfile=”myWebApp.war” basedir=”myproject/root”
Webxml=myproject/myWebApp.xml>
<lib dir=”myproject/libraries”/>
<classes dir=”myproject/classes”/>
</war>

Ear创建企业归档文件
<ear earfile=”myear.ear” basedir=”myproject/root”
Appxml=”myproject/myApp.xml” include=”*.jar,*.war”/>


使用ant任务的示例:
没有指定xml文件的时候,使用当前目录中的bulid.xml文件
<ant antfile=”mysubproject/mybulid.xml”/>
<ant dir=”myprojects/mysubproject”/>
<ant antfile =”bulidEJB.xml”>
<property name=””deployFile” value=”testEJB.jar”/>
</ant>
tanggoodboy 2008-07-29
  • 打赏
  • 举报
回复
五、 业务逻辑层



在业务逻辑层需要认真思考每个业务逻辑所能用到的持久层对象和DAO。DAO层之上是业务逻辑层,DAO类可以有很多个,但业务逻辑类应该只有一个,可以在业务逻辑类中调用各个DAO类进行操作。

1、创建服务接口类IBookService

1、 创建服务接口类IBookService
package com.sterning.books.services.iface;

import java.util.List;

import com.sterning.books.model.Books;

public interface IBooksService {
List getAll();//获得所有记录
List getBooks(int pageSize, int startRow);//获得所有记录
int getRows();//获得总行数
int getRows(String fieldname,String value);//获得总行数
List queryBooks(String fieldname,String value);//根据条件查询
List getBooks(String fieldname,String value,int pageSize, int startRow);//根据条件查询
Books getBook(String bookId);//根据ID获得记录
String getMaxID();//获得最大ID值
void addBook(Books pd);//添加记录
void updateBook(Books pd);//修改记录
void deleteBook(String bookId);//删除记录
}
com.sterning.books.services.iface.IBookService.java
2、实现此接口类:BookService:

package com.sterning.books.services;

import java.util.List;

import com.sterning.books.dao.iface.BooksDao;
import com.sterning.books.model.Books;
import com.sterning.books.services.iface.IBooksService;

public class BooksService implements IBooksService{
private BooksDao booksDao;

public BooksService(){}

/** *//**
* 函数说明:添加信息
* 参数说明:对象
* 返回值:
*/
public void addBook(Books book) {
booksDao.addBook(book);
}

/** *//**
* 函数说明:删除信息
* 参数说明: 对象
* 返回值:
*/
public void deleteBook(String bookId) {
Books book=booksDao.getBook(bookId);
booksDao.deleteBook(book);
}

/** *//**
* 函数说明:获得所有的信息
* 参数说明:
* 返回值:信息的集合
*/
public List getAll() {
return booksDao.getAll();
}

/** *//**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public int getRows() {
return booksDao.getRows();
}

/** *//**
* 函数说明:获得所有的信息
* 参数说明:
* 返回值:信息的集合
*/
public List getBooks(int pageSize, int startRow) {
return booksDao.getBooks(pageSize, startRow);
}

/** *//**
* 函数说明:获得一条的信息
* 参数说明: ID
* 返回值:对象
*/
public Books getBook(String bookId) {
return booksDao.getBook(bookId);
}

/** *//**
* 函数说明:获得最大ID
* 参数说明:
* 返回值:最大ID
*/
public String getMaxID() {
return booksDao.getMaxID();
}

/** *//**
* 函数说明:修改信息
* 参数说明: 对象
* 返回值:
*/
public void updateBook(Books book) {
booksDao.updateBook(book);
}

/** *//**
* 函数说明:查询信息
* 参数说明: 集合
* 返回值:
*/
public List queryBooks(String fieldname,String value) {
return booksDao.queryBooks(fieldname, value);
}

/** *//**
* 函数说明:获得总行数
* 参数说明:
* 返回值:总行数
*/
public int getRows(String fieldname,String value) {
return booksDao.getRows(fieldname, value);
}

/** *//**
* 函数说明:查询信息
* 参数说明: 集合
* 返回值:
*/
public List getBooks(String fieldname,String value,int pageSize, int startRow) {
return booksDao.getBooks(fieldname, value,pageSize,startRow);
}

public BooksDao getBooksDao() {
return booksDao;
}

public void setBooksDao(BooksDao booksDao) {
this.booksDao = booksDao;
}

}
tanggoodboy 2008-07-29
  • 打赏
  • 举报
回复
三、 建立数据持久化层

1、编写实体类Books及books.hbm.xml映射文件。

package com.sterning.books.model;

import java.util.Date;

public class Books {
// Fields
private String bookId;//编号
private String bookName;//书名
private String bookAuthor;//作者
private String bookPublish;//出版社
private Date bookDate;//出版日期
private String bookIsbn;//ISBN
private String bookPage;//页数
private String bookPrice;//价格
private String bookContent;//内容提要

// Constructors
public Books(){}

// Property accessors

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookAuthor() {
return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}

public String getBookContent() {
return bookContent;
}

public void setBookContent(String bookContent) {
this.bookContent = bookContent;
}

public Date getBookDate() {
return bookDate;
}

public void setBookDate(Date bookDate) {
this.bookDate = bookDate;
}

public String getBookIsbn() {
return bookIsbn;
}

public void setBookIsbn(String bookIsbn) {
this.bookIsbn = bookIsbn;
}

public String getBookPage() {
return bookPage;
}

public void setBookPage(String bookPage) {
this.bookPage = bookPage;
}

public String getBookPrice() {
return bookPrice;
}

public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}

public String getBookPublish() {
return bookPublish;
}

public void setBookPublish(String bookPublish) {
this.bookPublish = bookPublish;
}
}
com.sterning.books.model.Books.java
接下来要把实体类Books的属性映射到books表,编写下面的books.hbm.xml文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.sterning.books.model.Books" table="books" >
<id name="bookId" type="string">
<column name="book_id" length="5" />
<generator class="assigned" />
</id>
<property name="bookName" type="string">
<column name="book_name" length="100" />
</property>
<property name="bookAuthor" type="string">
<column name="book_author" length="100" />
</property>
<property name="bookPublish" type="string">
<column name="book_publish" length="100" />
</property>
<property name="bookDate" type="java.sql.Timestamp">
<column name="book_date" length="7" />
</property>
<property name="bookIsbn" type="string">
<column name="book_isbn" length="20" />
</property>
<property name="bookPage" type="string">
<column name="book_page" length="11" />
</property>
<property name="bookPrice" type="string">
<column name="book_price" length="4" />
</property>
<property name="bookContent" type="string">
<column name="book_content" length="100" />
</property>
</class>
</hibernate-mapping>
com.sterning.books.model.books.hbm.xml
2、hibernate.cfg.xml配置文件如下:(注意它的位置在scr/hibernate.cfg.xml)

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>

<mapping resource="com/sterning/books/model/books.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
tanggoodboy 2008-07-29
  • 打赏
  • 举报
回复
转一struts2 spring hibernate整合的文章:
Struts2+Spring2+Hibernate3 web应用示例(一)
Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点:MVC 2模型的使用、功能齐全的标志库(Tag Library)、开放源代码。而Spring的出现,在某些方面极大的方面了Struts的开发。同时,Hibernate作为对象持久化的框架,能显示的提高软件开发的效率与生产力。这三种流行框架的整合应用,可以发挥它们各自的优势,使软件开发更加的快速与便捷。

struts2发布已经很久了,但关于如何使用它的教程及实例并不多。特别是与Spring及Hibernate等流行框架的集成,并不多见。现在就将笔者使用Myeclipse工具应用struts2 + spring2 + hibernate3 实现CRUD操作的步骤一一纪录下来,为初学者少走弯路略尽绵薄之力!在本文中,笔者将Struts2.0.6、Spring2.0.6和Hibernate3.1进行整合,希望通过这样的整合示例,让读者了解这些框架各自的特点,以便于在自己的项目中,根据实际情况,尽快的过渡到Struts2的时代。本文的内容基于Struts2.0.6。


一、 准备工作

spring2与1.x区别不大,可以平滑的过度,笔者也是把spring1.28换成了spring2.0.6,算是升级到spring 2.0了。struts2基本就是webwork2.2,与以前的struts1.x可以说没任何关系了。因为是第一次用struts2,也是第一次用webwork,所以有很多不完善,不规范的地方,还望大家来拍砖。

开发环境:MyEclipse5.0+Eclipse3.2+JDK5.0+

Tomcat5.5+struts2+Spring2.0.6+Hibernate3.1。本示例通过对一个图书进行管理的系统,提供基本的增加、删除、修改、查询等功能。

lib包需要以下右图所示的这些包。其中Struts2.0.6的下载地址为:



http://people.apache.org/builds/struts/2.0.6

Hibernate3.1的下载地址为:

http://www.hibernate.org

spring2.0.6的下载地址为:

http://www.springframework.org

使用的数据库为mysql 5.0,使用的JDBC驱动JAR包为:mysql-connection-java-5.0.4-bin

创建数据表的sql语句为:

create database game

CREATE TABLE `books` (
`book_id` int(11) NOT NULL default '0',
`book_name` varchar(200) character set gb2312 default NULL,
`book_author` varchar(100) character set gb2312 default NULL,
`book_publish` varchar(100) character set gb2312 default NULL,
`book_date` date default NULL,
`book_isbn` varchar(20) default NULL,
`book_page` int(11) default NULL,
`book_price` decimal(10,2) default NULL,
`book_content` varchar(100) character set gb2312 default NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=COMPRESSED;


二、 建立公共类

1、AbstractAction类


Struts2和Struts1.x的差别,最明显的就是Struts2是一个pull-MVC架构。Struts1.x 必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。

虽然,在理论上Struts2的Action无须实现任何接口或者是继承任何的类,但是,在实际编程过程中,为了更加方便的实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并且重载(Override)

package com.sterning.commons;

import com.opensymphony.xwork2.ActionSupport;

public class AbstractAction extends ActionSupport {
}

com.sterning.commons.AbstractAction.java

参考JavaDoc,可知ActionSupport类实现了接口:

com.opensymphony.xwork2.Action

com.opensymphony.xwork2.LoaleProvider

com.opensymphony.xwork2.TextProvider

com.opensymphony.xwork2.Validateable

com.opensymphony.xwork2.ValidationAware

com.uwyn.rife.continuations.ContinuableObject

java.io.Searializable

java.lang.Cloneable

2、Pager分页类

为了增加程序的分页功能,特意建立共用的分页类。


package com.sterning.commons;

import java.math.*;

public class Pager {
private int totalRows; //总行数
private int pageSize = 5; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行

public Pager() {
}

public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;
}

public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
}

com.sterning.commons.Pager.java

同时,采用PagerService类来发布成为分页类服务PagerService,代码如下:


同时,采用PagerService类来发布成为分页类服务PagerService,代码如下:
package com.sterning.commons;

public class PagerService {
public Pager getPager(String currentPage,String pagerMethod,int totalRows) {
// 定义pager对象,用于传到页面
Pager pager = new Pager(totalRows);
// 如果当前页号为空,表示为首次查询该页
// 如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
// 获取当前执行的方法,首页,前一页,后一页,尾页。
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
}
freedom2001 2007-07-01
  • 打赏
  • 举报
回复
An uncaught exception was thrown during actionExecute()
actionExecute() 的时候有未捕捉的exception抛出了
你不会是光写程序,不try catch吧

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧