Spring+struct2+Hibernate 框架网上商城项目整合篇

目录结构:



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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置连接池 -->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置c3p0连接池 /就是获取数据源从jdbc.properties属性文件里边获得参数-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
     <property name="driverClass" value="${jdbc.driver}"/>
     <property name="jdbcUrl" value="${jdbc.url}"/>
     <property name="user" value="${jdbc.user}"/>
     <property name="password" value="${jdbc.password}"/>
    </bean>
   <!-- 配置Hibernate的一些相关信息 -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <!-- 注入连接池 -->
      <property name="dataSource" ref="dataSource"/>
      <!-- 配置Hibernate的其他的属性 -->
      <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
   </property>
      <!-- 配置Hibernate的映射文件 -->
       
   
   </bean>
    <!-- 事务管理: -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- Action的配置 ============================-->
  <!-- 首页访问的Action -->
   <bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype">
    
   </bean>
   <!-- 配置验证码的Action -->


   
   <!-- 用户模块的Action -->
            <bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype">
           
            </bean>



  
    
<!-- Service的配置============================ -->



<!-- Dao的配置 ============================-->
 
</beans>


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ChuSe</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <!-- 核心监听器 -->
    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
 <!-- 配置Structs2的核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
   <dispatcher>FORWARD</dispatcher>
  </filter-mapping>
</web-app>

structs.xml:

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


<struts>
   


    <constant name="struts.devMode" value="false" />
   
<package name="shop2" extends="struts-default" namespace="/">
       <!-- 配置首页访问的Action -->
   <action  name="index" class="indexAction">
     <result name="index">/WEB-INF/jsp/index.jsp</result>
   </action>
   <!-- 配置用户模块的Action -->
     
        </action>
   
    </package>
</struts>







@Namespace("") @Results({ @Result(name = "loginSuccess", location = "/WEB-INF/success.jsp"), @Result(name = "loginFailure", location = "/WEB-INF/failure.jsp"), @Result(name = "checkSuccess", location = "/WEB-INF/success.jsp"), @Result(name = "checkFailure", location = "/WEB-INF/failure.jsp") }) public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String userName; private String passWord; private String resultMsg; private int resultInt; RegisterImpl regImpl = new RegisterImpl(); // 提交注册 @Action("register") public String register() { // 验证用户名是否重复 resultInt = regImpl.findByUserName(userName); if (resultInt != 0) { resultMsg = "用户名已被使用,请重新输入"; return "registerFailure"; } else { resultInt = regImpl.register(userName, passWord); if (1 == resultInt) { // 1. 将用户信息保存到Session中。 Map<String, Object> session = ActionContext.getContext().getSession(); session.put("userName", userName); session.put("passWord", passWord); // 2. taglib标识传递 resultMsg = "注册成功"; return "registerSuccess"; } else { resultMsg = "注册失败"; return "registerFailure"; } } } // 登录 @Action("login") public String login() { resultMsg = null; // 对用户的输入格式进行验证//应定义在jsp页面javScript if ("".equals(userName) || userName == null) { resultMsg = "用户名不能为空"; return "checkFailure"; } else if ("".equals(passWord) || passWord == null) { resultMsg = "密码不能为空"; return "checkFailure"; } else { if (passWord.length() < 4 || passWord.length() > 10) { resultMsg = "密码必须在4到10之间"; return "checkFailure"; } } // 数据库查询,根据用户输入与数据库中数据匹配情况 SelectConOper selectConOpe = new SelectConOper(); int resultInt = selectConOpe.selectStatement(userName, passWord); // int resultInt = // selectConOpe.selectPreparedStatement(userName,passWord); if (1 == resultInt) { resultMsg = "登录成功"; return "loginSuccess"; } else { resultMsg = "登录失败"; return "loginFailure"; } } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getResultMsg() { return resultMsg; } public void setResultMsg(String resultMsg) { this.resultMsg = resultMsg; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值