我用的开发环境:
jdk1.5
eclipse3.12
myeclipse 4.1
spring 1.2
hibernate3.0
数据库服务器:sqlserver2000
数据库驱动: jtds1.2
web服务器:toncat5.0
我要实现的功能是:当用户登陆后显示一个login连接,当用户单击这个连接时程序执行LoginAction.java
以下是程序的目录结构:
src
---com
----test
---hibernate
----AbstractUser.java
----SessionFactory.java
----User.hbm.xml
----User.java
----struts
--action
LoginAction.java
-----UserDao.java
-----UserDaoIMP.java
-----WebRoot
----index.jsp
以下是applicationContext.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>net.sourceforge.jtds.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:jtds:sqlserver://192.168.1.171:1433;DatabaseName=test;SelectMethod=cursor</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/test/Hibernate/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userDAO" class="com.test.UserDAOImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="userDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean name="/login" class="com.test.struts.action.LoginAction" singleton="false">
<property name="userDAO">
<ref bean="userDAOProxy" />
</property>
</bean>
</beans>
UserDao.java的代码:
package com.test;
public interface UserDAO {
public abstract boolean isValidUser(String username, String password);
}
UserDaoIMP.java的代码:
package com.test;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImp extends HibernateDaoSupport implements UserDAO {
private static String hql = "from User u where u.username=? and password=?";
public boolean isValidUser(String username, String password) {
String[] userlist=new String[2];
userlist[0]=username;
userlist[1]=password;
// List userList = this.getHibernateTemplate().find(hql, (Object)username);
List userList = this.getHibernateTemplate().find(hql,userlist);
if (userList.size() > 0) {
return true;
}
return false;
}
}
LoginAction.java的代码:
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.test.UserDAO;
public class LoginAction extends Action {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
if (userDAO.isValidUser("test","test")) {
return mapping.getInputForward();
} else {
return mapping.getInputForward();
}
}
}
index.jsp的内容:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
<a href="<%=basePath%>login.do">login</a>
</body>
</html>
以下是hibernate自动生成的User.hbm.xml,hibernate.cfg.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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.test.Hibernate.User" table="user" schema="dbo" catalog="test">
<id name="id" type="integer">
<column name="ID" />
<generator class="sequence" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="50" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="50" />
</property>
</class>
</hibernate-mapping>
struts-config.xml的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
让人不解的是,当我点击login连接时,出现了以下错误信息:
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
--------------------------------------------------------------------------------
当我把applicationContext.xml改成以下后:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring
-beans.dtd">
<beans>
<bean name="/login" class="com.test.struts.action.LoginAction" singleton="false">
</bean>
</beans>
程序能够正常运行并执行到了我设在LoginAction.java中的段点,由此看来,应该是
applicationContext.xml的配置出了问题,
但不知道问题出在什么地方?是不是数据源配错了??