一個有關struts+spring+hibernate專案的問題,

熱血青年發表於2006-09-10
我這幾天在用struts+spring+hibernate寫了一個專案。我的環境配置是eclipse3.2 jdk1.5 tomcat5.0.28 struts1.2.9 spring1.2.7 hibernate3.我在除錯的時候總是丟擲錯誤,以下是我的配置檔案
《application-Context.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="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>"com.managerFiler.dao.model.File.hbm.xml"</value>
<value>"com.managerFiler.dao.model.User.hbm.xml"</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="fileDAO" class="com.managerFile.dao.Impl.FileDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userDAO" class="com.managerFile.dao.Impl.UserDAOImpl">
<property name="sessionFactory">
<ref bean="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="**">PROPAGATION_REQUIRED</prop>
<prop key="**">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="**">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="fileDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="fileDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="**">PROPAGATION_REQUIRED</prop>
<prop key="**">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="**">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>

</bean>
<bean id="fileService"
class="com.managerFile.service.impl.FileServiceImpl">

<property name="fileDAO">
<ref bean="fileDAO" />
</property>
</bean>
<bean id="userService"
class="com.managerFile.service.impl.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO" />
</property>
</bean>
<bean name="/loginAction" class="com.managerFile.action.LoginAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>
<bean name="/indexAction" class="com.managerFile.action.IndexAction">
<!-- <property name="userService">
<ref bean="userService"/>
</property>
<property name="fileService">
<ref bean="fileService"/>
</property>-->
</bean>
<bean name="/registAction" class="com.managerFile.action.RegistAction">
<!-- <property name="userService">
<ref bean="userService"/>
</property>-->

</bean>
<bean name="/up_FileAction" class="com.managerFile.action.UpFileAction">
<!-- <property name="userService">
<ref bean="userService"/>
</property>
<property name="fileService">
<ref bean="fileService"/>
</property>-->
</bean>
<bean name="/fileManagerAction" class="com.managerFile.action.FileManagerAction">
<!-- <property name="fileService">
<ref bean="fileService"/>
</property>-->
</bean>

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/mydb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123-789</value>
</property>
</bean>
</beans>

<<web.xml>>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app >

<display-name >ManagerFile</display-name>
<context-param >
<param-name >contextConfigLocation</param-name>
<param-value >/WEB-INF/application-Context.xml</param-value>
</context-param>

<!-- <listener >
<listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<servlet>
<servlet-name>
context
</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet >
<servlet-name >action</servlet-name>
<servlet-class >org.apache.struts.action.Actionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping >
<servlet-name >action</servlet-name>
<url-pattern >*.do</url-pattern>
</servlet-mapping>

<welcome-file-list >
<welcome-file >welcome.jsp</welcome-file>
<welcome-file>/WEB-INF/pages/login.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri >/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location >/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri >/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location >/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri >/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location >/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
</web-app>

<<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>
<form-beans >
<form-bean name="LoginForm" type="com.managerFiler.form.LoginForm" />
<form-bean name="registForm" type="com.managerFiler.form.RegistForm" />
<form-bean name="upFileForm" type="com.managerFiler.form.UpFileForm" />
<form-bean name="managerFilerForm" type="com.managerFiler.form.ManagerFilerForm" />
</form-beans>
<global-forwards>
<forward name="welcome" path="loginAction.do" ></forward>
</global-forwards>
<action-mappings>
<action
input="/WEB-INF/pages/login.jsp"
name="LoginForm"
path="/loginAction"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="index" path="/WEB-INF/pages/index.jsp" />
<forward name="login" path="/WEB-INF/pages/login.jsp" />
</action>
<action
input="/WEB-INF/pages/regist.jsp"
name="registFrom"
path="/registAction"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="index" path="/WEB-INF/pages/index.jsp" />
<forward name="regist" path="/WEB-INF/pages/regist.jsp" />
</action>
<action
path="/indexAction"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="false">
<forward name="index" path="/WEB-INF/pages/index.jsp" />
</action>
<action path="/fileManagerAction" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="succeess" path="/WEB-INF/pages/success.jsp" />
<forward name="farilur" path="/WEB-INF/pages/farilur.jsp" />
</action>
<action
input="/WEB-INF/pages/up_File.jsp"
name="upFileForm"
path="/up_FileAction"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="farilur" path="/WEB-INF/pages/up_File.jsp" />
<forward name="success" path="/WEB-INF/pages/index.jsp" />
</action>
<action path="/fileManager" input="/WEB-INF/pages/fileManager.jsp" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="farilur" path="/WEB-INF/pages/fileManager.jsp"></forward>
<forward name="success" path="/WEB-INF/pages/index.jsp"></forward>
</action>
<action path="/regist" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/regist.jsp" scope="request"></action>
<action path="/up_File" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/up_File.jsp" scope="request"></action>
<action path="/down_File" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/down_File.jsp" scope="request"></action>
<action path="/fileManager" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/fileManager.jsp" scope="request"></action>
<action path="/logout" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/logout.jsp" scope="request"></action>
</action-mappings>
<!-- <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"> </controller>-->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/application-Context.xml"/>
</plug-in>
</struts-config>


package com.managerFile.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.managerFile.dao.model.User;
import com.managerFile.service.UserService;
import com.managerFile.web.form.LoginForm;

public class LoginAction extends BaseAction {
@SuppressWarnings("deprecation")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
LoginForm lForm=(LoginForm) form;
String user_Name=lForm.getUser_Name();
String user_PassWord=lForm.getPassWord();
ActionErrors errors=new ActionErrors();
UserService userService=null;

try{
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
userService=(UserService) ctx.getBean("userService");
User user=userService.getUser(user_Name);
if(user.getUser_Name().equals(null)){

return mapping.findForward("login");

}
if(user.getUser_PassWord().equals(null)){
return mapping.findForward("login");
}

if(user.getUser_Name().equals(user_Name)||user.getUser_PassWord().equals(user_PassWord)){
return mapping.findForward("index");
}
else{
return mapping.findForward("login");
}
}
catch(Exception ex){
ex.printStackTrace();
errors.add("name", new ActionError("id"));
}
return null;
}
}

<<一個action>>yywrpackage com.managerFile.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.managerFile.dao.model.User;
import com.managerFile.service.UserService;
import com.managerFile.web.form.LoginForm;

public class LoginAction extends BaseAction {
@SuppressWarnings("deprecation")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
LoginForm lForm=(LoginForm) form;
String user_Name=lForm.getUser_Name();
String user_PassWord=lForm.getPassWord();
ActionErrors errors=new ActionErrors();
UserService userService=null;

try{
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
userService=(UserService) ctx.getBean("userService");
User user=userService.getUser(user_Name);
if(user.getUser_Name().equals(null)){

return mapping.findForward("login");

}
if(user.getUser_PassWord().equals(null)){
return mapping.findForward("login");
}

if(user.getUser_Name().equals(user_Name)||user.getUser_PassWord().equals(user_PassWord)){
return mapping.findForward("index");
}
else{
return mapping.findForward("login");
}
}
catch(Exception ex){
ex.printStackTrace();
errors.add("name", new ActionError("id"));
}
return null;
}
}

請各位大俠幫助小弟看看是哪配置不對,每一次配置都說servlet action 不對。小弟在此謝謝大家了
<<一個jsp檔案>>

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!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=gb2312">
<title>無標題文件</title>
</head>

<body>
<form name="LoginFrom" method="post" action="loginAction.do">
  <p>&nbsp;</p>
  <label>
  <div align="center">
    <pre>
     
     
     
     使用者名稱     <input name="userName" type="text" align="middle" height="20">     


 密 碼      <input name="userPassWord" type="password" height="20"> 
 
 
 
                   <input type="submit" name="Submit" value="登入">   <input type="reset" name="Submit2" value="重置"> <a href="regist.do">請先註冊</a>    </pre>
    <div align="right">
      <pre>&nbsp;
    </pre>
    </div>
  </div>
  </label>
</form>
</body>
</html>
<p class="indent">

相關文章