struts1.3 spring2.5 hibernate3.2问题解决过程

1、创建数据库的时候,我用的是mysql,配置数据库编码方式必须是gbk的,不然客户端查询的全是乱码。

2、网页上最好统一使用utf-8不然也会出现乱码,当我在数据库中插入的登录用户名是汉字后,从网页上输入汉字用户名登录,会出现无法登录,原因是编码方式不一样,做个转换就可以了,如下所示

username = new String(username.getBytes("iso-8859-1"),"utf-8");

这是因为我们输入的用户名是windows的默认的iso-8859-1方式的,要将它转换成utf-8,这样再与数据库中的数据比较才不会出现找不到明明是相同的字串的问题。

3、由于spring不太会用,我直接在Action里面调用DAO,发现出现空指针异常,

从网上找了好久,发现这句可以成功解决

   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserloginDAO logindao = (UserloginDAO)context.getBean("UserloginDAO");
   List list = logindao.findByUsername(username);

4、再贴出表单验证的代码,可能写的不符合规范,也不管了,先把ssh柜架走通再说。

public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
   ActionErrors errors = new ActionErrors();
   HttpSession session = request.getSession();
   if(loginName == null || loginName.trim().length() < 1)
   {
    errors.add("loginNamenull", new ActionMessage("loginNamenull"));
   }
   else
   {
    session.setAttribute("username", loginName);
   }
   if(password == null || password.trim().length() < 1)
   {
    errors.add("passwordnull", new ActionMessage("passwordnull"));
   }
   else
   {
    session.setAttribute("password", password);
   }
  
   // TODO Auto-generated method stub
   return errors;
}

5、下面再贴出applicationContext.xml的代码

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean id="datasource"
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
   </property>
   <property name="url"
    value="jdbc:mysql://localhost:3306/(此处请填写您的数据库名称)">
   </property>
   <property name="username" value="root"></property>
   <property name="password" value="123456"></property>
</bean>
<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>
     <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
   </property>
   <property name="mappingResources">
    <list>
     <value>hibernate/Competer.hbm.xml</value>
     <value>hibernate/Customer.hbm.xml</value>
     <value>hibernate/Project.hbm.xml</value>
     <value>hibernate/Proxy.hbm.xml</value>
     <value>hibernate/Purview.hbm.xml</value>
     <value>hibernate/Userlogin.hbm.xml</value></list>
   </property></bean>
  
   <!--
<bean name="/validateUser"
    class="beans.AdminLoginForm">
    <property name="loginName" ref="UserloginDAO" />
</bean>
-->
  
<bean id="CompeterDAO" class="hibernate.CompeterDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<bean id="CustomerDAO" class="hibernate.CustomerDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<bean id="ProjectDAO" class="hibernate.ProjectDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<bean id="ProxyDAO" class="hibernate.ProxyDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<bean id="PurviewDAO" class="hibernate.PurviewDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<bean id="UserloginDAO" class="hibernate.UserloginDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean></beans>

 

6、再贴上hibernate.cfg.xml的代码

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
   <property name="connection.username">root</property>
   <property name="connection.url">
    jdbc:mysql://localhost:3306/(此处请填写您的数据库名称)
   </property>
   <property name="dialect">
    org.hibernate.dialect.MySQLDialect
   </property>
   <property name="myeclipse.connection.profile">
    com.mysql.jdbc.Driver
   </property>
   <property name="hbm2ddl.auto">update</property>
   <property name="connection.password">123456</property>
   <property name="connection.driver_class">
    com.mysql.jdbc.Driver
   </property>

   <mapping resource="hibernate/Competer.hbm.xml" />
   <mapping resource="hibernate/Customer.hbm.xml" />
   <mapping resource="hibernate/Project.hbm.xml" />
   <mapping resource="hibernate/Proxy.hbm.xml" />
   <mapping resource="hibernate/Purview.hbm.xml" />
   <mapping resource="hibernate/Userlogin.hbm.xml" />
</session-factory>

</hibernate-configuration>

 

6、再贴出struts-config.xml的代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans>
<form-bean name="adminloginform" type = "beans.AdminLoginForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="indexjsp" path = "/index.jsp"></forward>
</global-forwards>
<action-mappings>
<action path="/adminlogin"
type="actions.AdminLoginAction" name="adminloginform"
validate="true" input="/adminlogin.jsp">
   <!--
   type="com.wuwei.struts.action.ValidateUserAction" name="userForm"
   type="org.springframework.web.struts.DelegatingRequestProcessor" name="userForm"
    -->
   
    <forward name="success" path="/index.jsp"></forward>
    <forward name="fail" path="/adminlogin.jsp"></forward>
   </action>
   <action path="/needtoconfirm"
type="actions.NeedToConfirm" >
    <forward name="success" path="/proProcess.jsp"></forward>
   </action>
   <action path="/alreadyconfirm"
type="actions.AlreadyConfirm" >
    <forward name="success" path="/proProcess2.jsp"></forward>
   </action>
   <action path="/highquery"
type="actions.HighQuery" >
    <forward name="success" path="/query.jsp"></forward>
   </action>
   <action path="/proxysellermanage"
type="actions.ProxySellerManage" >
    <forward name="success" path="/adminresellerregister.jsp"></forward>
   </action>
   <action path="/passwordmodify"
type="actions.PasswordModify" >
    <forward name="success" path="/yzLoginAction.jsp"></forward>
   </action>

</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
       <set-property property="contextConfigLocation" value="classpath*:applicationContext.xml" />
      
</plug-in>
</struts-config>

7、再贴出web.xml的代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>adminlogin.jsp</welcome-file>
</welcome-file-list>
</web-app>

我才会那么一点点,希望同行们多多指教!

你可能感兴趣的:(spring,Hibernate,bean,struts,Class,Path)