慕工程9204341
2017-09-14 18:15
/** * 商品管理的Action类 */ public class ProductAction extends ActionSupport implements ModelDriven<Product>{ //模型驱动使用的类 private Product product = new Product(); @Override public Product getModel() { return product; } // Struts和Spring整合过程中按名称自动注入的业务层类 private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } /** * 保存商品的执行方法:save */ public String save(){ System.out.println("Action中的save方法执行了..."); productService.save(product); return NONE; } }
/**
* 商品管理的业务类
*/
@Transactional
public class ProductService {
//业务层注入DAO的类
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
/**
* 业务层保存商品的方法
* @param product
*/
public void save(Product product) {
System.out.println("Servie中的save方法执行了...");
productDao.save(product);
}
}
/**
* 商品管理的DAO类
*/
public class ProductDao extends HibernateDaoSupport{
/**
* DAO中保存商品的方法
* @param product
*/
public void save(Product product) {
System.out.println("DAO中save方法执行了...");
this.getHibernateTemplate().save(product);
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.CombopooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置Hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置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.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载Hibernate中的映射文件 -->
<property name="mappingResources">
<list>
<value>com/test/ssh/domain/Product.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置Action的类 -->
<bean id="productAction" class="com.test.ssh.action.ProductService" scope="prototype">
<!-- 手动注入Service -->
<property name="productService" ref="productService"></property>
</bean>
<!-- 配置业务层类 -->
<bean id="productService" class="com.test.ssh.service.ProductService">
<property name="productDao" ref="productDao"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="productDao" class="com.test.ssh.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager ">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="ssh" extends="struts-default" namespace="/">
<!-- <action name="product_*" class="com.test.ssh.action.ProductAction" method="{1}">
</action> -->
<action name="product_*" class="productAction" method="{1}">
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- Spring的框架的核心监听器 -->
<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>
<!-- Struts2的框架的核心过滤器的配置 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name>testSSH</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=UTF-8">
<title>添加商品</title>
</head>
<body>
<h1>保存商品的页面</h1>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<tr>
<td>商品名称</td>
<td><s:textfield name="pname"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><s:textfield name="price"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"/></td>
</tr>
</table>
</s:form>
</body>
</html>
这个应该是你服务器启动的时候项目就报错了,所以从服务器里面找,,我秀逗了都没想到
一样的问题,,不知道为什么
<!-- 配置Hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager ">
<property name="sessionFactory" ref="sessionFactory"></property>
这里会不会是包错误,老师用的是hibernate3的包哦....你如果用的是老师的包那肯定要改成hibernate3呀+ --
<!-- 配置hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置会话工厂(hibernate的其他相关属性以及关系映射文件) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
如果不是这个问题那你复制粘贴一下错误,截图也行,因为404可以是路径错误也可能是其他的....
理论上来说应该是:struts.xml文件内的product_*这里的class类路径配置错误
这里你也没贴出struts.xml文件的内容 无法判断
404就是路径问题,注意路径就OK了
基于SSH实现员工管理系统之框架整合篇
49831 学习 · 344 问题
相似问题