不要只做个ACV工程师,SSH框架配置文件详解。知其然,也要知其所以然。

SSH框架配置解析:

SSH分别代表:

  1. SpringMVC
  2. Spring(连接MVC和hibernate)
  3. Hibernate

SpringMVC.xml

【在这个配置文件中常见的问题】:

1.启动报错,扫描不到包:
	[解决办法]:检查base-package 扫描的包路径,需要写入全包路径。如示例中的base-package="com.ac.cloud.**.controller"。
	
2.在执行页面跳转时报错,404错误:
	[解决办法]:检查视图解析器,比如前缀里,value="/WEB-INF/views/,这里自己加了一层包结构views,如果我们没有手动创建这个包,或者需要跳转的jsp页面不在这个文件夹下,就会报错。同时在这个配置文件中还配置了后缀 (.jsp),我们在后台写跳转的时候,就可以省略这个后缀,直接写  views/文件名   ,这个文件名不用加.jsp后缀。
	
3.页面正常跳转了,但是没有加载页面的样式,只有文字内容,这是因为没有放开静态资源
	[解决办法]:我们可以把需要引用的CSS和JS等静态资源放开,在这里我是习惯把所有需要放开的静态资源统一放在一个名为static的包里,这样配置的话,直接一行代码解决,放开所有的静态资源。

<?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" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
    <!-- 配置文件描述 -->
	<description>Spring MVC Configuration</description>
	
	<!-- 自动注入,扫描注解 @Controller;base-package 扫描的包路径
	<context:component-scan base-package="com.ac.cloud.**.controller"></context:component-scan> -->
	
	<context:component-scan base-package="com.ac.cloud.**.controller" use-default-filters="false">
	<!-- base-package 如果多个,用“,”分隔  -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
  	<!-- 放开静态资源
	<mvc:resources location="/static/" mapping="/static/**"></mvc:resources> -->
	
	<!-- 静态资源映射 -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
    
    <!-- 对静态资源文件的访问, 将无法映射到Controller的path交给default servlet handler处理 -->
	<mvc:default-servlet-handler />
	
	<!-- 默认注解驱动 -->
	<mvc:annotation-driven />
	
</beans> 


Spring起到连接MVC和hibernate的作用,Spring.xml配置文件也经常被命名为ApplicationContext.xml,(简写为AppContext.xml)文件中的配置内容如下

AppContext.xml

【在这个配置文件中常见的问题】:

1.启动报错,文件未扫描到,关键词scan,扫描的意思
	[解决办法]:检查包路径,Spring扫描的是全包,base-package="com.ac.cloud",所以到这一级就行了。
	
2.事务的配置问题
	[解决办法]:注意看一下事务配置,我们自定义的增删改的名字,可以更改,add,delete,update,save,例如我不喜欢用delete这个单词,可以完全自定义为其他的,比如remove移除,只要自己能知道就行。除了这几个事务算是列了个白名单,可以执行,其他都是只读事务,下面一个配置的 * 星号代表所有。
	
3.AOP切点的问题
	[解决办法]:默认都是横切service层,写一些逻辑判断什么的。这里也是注意路径,可以使用 * 星号代表不区分某一层的包名

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 配置文件描述 -->
    <description>Spring Configuration</description>
    
    <!-- 自动注入,注入@Controller @Service @Repository;
    	base-package 扫描全包路径;
     -->
    <context:component-scan base-package="com.ac.cloud"></context:component-scan>
	
	
	<!-- 创建sessionFactory 交给spring管理 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>
		
	<!-- 创建hibernateTemplate 交给spring管理 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 定义一个事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
        
	<!-- 定义 advice 配置事务传播特性、事务隔离级别、只读事务、回滚策略 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
	 	<tx:attributes>
	 		<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
	 		<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
	 		<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
	 		<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
	 		<tx:method name="*" propagation="REQUIRED" read-only="true"/>
	 	</tx:attributes>
	</tx:advice>
       
	<!-- 切点配置:execution(* com.ac.service.impl.*.*(..))
	            第一个*:任意返回值
	            第二个*:包下任意的类
	            第三个*:类中的所有方法 (..):任意参数 
	     默认横切Service业务层
	-->
	<aop:config>
		<aop:pointcut expression="execution(* com.ac.cloud.*.service.impl.*.*(..))" id="servicePointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
	</aop:config>
	
</beans> 

hibernate是JDBC的重量级封装,主要用来连接数据库,咱们来看一下里边的配置就明白了。

hibernate.xml

【在这个配置文件中常见的问题】:

1.数据库地址错误
[解决办法]:jdbc:mysql://localhost:3306/ac? ,localhost可以换为目标ip地址,3306是mysql数据库默认端口号,一般情况下不用改,ac是自己定义的数据库名,必须要改为自己的数据库名称。

2.启动报错,映射文件未找到,数据库没有自动创建表。
[解决办法]:只要是报No mapping,或者mapped关键字的错误,我认为十有八九就是在静态资源映射里没有配置咱们的hbm.xml文件,在加载hibernate的映射文件中,写入自己的映射文件地址。如果有多个映射文件,把标签行复制一下,改地址就行了。


<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- 数据库驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		
		<!-- 数据库地址、用户名、密码 -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ac?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8&amp;amp;</property>
		<property name="hibernate.connection.username">数据库用户名:例如 root</property>
		<property name="hibernate.connection.password">数据库的密码:例如 123456</property>
		
		<!-- 数据库方言,用于让hibernate在运行时根据方言生成不同的SQL语句,从而去兼容不同的数据库。 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 配置其它hibernate相关属性 -->
		
		<!-- 是否显示SQL语句 -->
		<property name="hibernate.show_sql">true</property>
		
		<!-- 是否对SQL语句格式化 -->
		<property name="hibernate.format_sql">true</property>
		
		<!-- 自动建表
			create:始终会自动创建表,如果表存在就把删了再建
			create-drop:自动创建表,在调用了sessionFactory.close方法的时候再把表删掉
			update:如果数据库没有这个表那么就创建,如果有这张表就修改这张表的表结构
		 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 加载hibernate映射文件  -->
		<mapping resource="这里写映射文件的全类名,比如:com/ac/cloud/car/hbm/car.hbm.xml"></mapping>
		
	</session-factory>
	
</hibernate-configuration>


以上是结合自身实际遇到的问题,对SSH框架配置文件的一点理解。期待各位大佬批评指正,帮我找到不足!

(内容原创手打,转载请注明,这样我会更有创作动力的)

我是李斯特 你早晚要关注我的!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员良哥

我看你骨骼惊奇,花钱买点知识吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值