springside 学习日志

Springside学习笔记

数据层:

第一步:我们要配置好数据源:

src/resources文件夹下面创建jdbc.properties文件

(这里我们使用mysql作为我们的数据库)

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc/:mysql/://localhost/message?useUnicode/=true&characterEncoding/=utf-8

 

jdbc.username=root

jdbc.password=123

 

配置好数据库驱动类,url,以及帐号和密码

 

将配置好的数据源配置到spring框架中

1).我们将该数据源文件配置成一个属性文件作读入

bean的形式设置在applicationContext.xml文件中

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

       <property name="locations">

           <list>

              <value>classpath*:config/jdbc.properties</value>

           </list>

       </property>

    </bean>

 

2)spring的另一个配置文件dataAccessContext-ibatis.xml

我们利用上面的数据源,配置数据库连接池

    <bean id="dataSource"

       class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close">

       <property name="driverClassName"

           value="${jdbc.driverClassName}" />

       <property name="url" value="${jdbc.url}" />

       <property name="username" value="${jdbc.username}" />

       <property name="password" value="${jdbc.password}" />

    </bean>

 

注意:使用dbcp数据库连接池,相应的Jar包要引入进来

 

3).将此dataSourc配置成另一个bean dataSource属性 

<bean id="sqlMapClient"

       class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

       <property name="configLocation">

           <value>classpath:sql-map-config.xml</value>

       </property>

       <property name="dataSource">

           <ref bean="dataSource" />

       </property>

    </bean>

注意:上面的bean中的configLocation的值设为:sql-map-config.xml,这个文件中不配置数据源,但里面配好了一些实体(POJO)映射配置文件.

<sqlMapConfig>

    <!-- 添加对象映射文件 -->

  <sqlMap resource="com/ib/message/model/mapping/message.xml" />

 

</sqlMapConfig>

而真正设置数据源则由它的dataSource属性指定,这个属性又由上面的dataSource相对引用面得到.

 

第二步:事务管理:

1.iBatis事务

iBatis中注入TransactionManager(即事务管理器)

(dataAccessContext-ibatis.xml)

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource" />

    </bean>

2.spring框架中事务管理(applicationContext.xml文件中)

<!-- 支持 @Transactional 标记 -->

    <tx:annotation-driven/>

 

 

    <!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.

            默认的设置请参考Spring文档事务一章. -->

    <tx:advice id="txAdvice">

       <tx:attributes>

           <tx:method name="get*" read-only="true"/>

           <tx:method name="find*" read-only="true"/>

           <tx:method name="*"/>

       </tx:attributes>

    </tx:advice>

 

    <!-- 支持 @AspectJ 标记-->

    <aop:aspectj-autoproxy/>

 

    <!-- AspectJ方式 定义 AOP -->

    <aop:config proxy-target-class="true">

       <!-- 注意,请把第2*号换为项目package -->

<aop:advisor pointcut="execution(* *..service.*Manager.*(..))" advice-ref="txAdvice"/>

    <aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>

    </aop:config>

 

 

然后在serviceContext.xml文件中就可以注册ServerManager类了

<!-- 这里定义*ManagerBean -->

<bean

id="messageManager"

class="com.ib.message.service.MessageManager"/>

 

第三步:业务方法

 

分层结构:

在第一层中定义Message.java实体类,也就是一些getter/setter方法集

类要实现Serializable接口

。。。。。。。。。。。。。。。。

public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

 

    public String getName() {

       return name;

    }

。。。。。。。。。。。。。。。

 

在第二层:

定义message.xml映射配置文件

这个是属于iBatis框架中的重要文件

里面可以定义select,insert,update,delete,还有存储过程等

预先写好该配置SQL文件,为下面提拱使用

例如:

<sqlMap namespace="Message">

    <insert id="com.ib.message.model.Message.insert" parameterClass="com.ib.message.model.Message">

       insert into message (name,email,title,time,content) value

       (#name#,#email#,#title#,#time#,#content#)

</insert>

</sqlMap>

注意要对数据库中对应的表相一致,符合SQL语言的规范等

 

第三层:服务类包

这里面的类继承了org.springside.core.dao.IbatisEntityDao抽象类

是一些业务逻辑方法,为下层的action包提拱实现方法.

例如: (下面是一个查询方法)---

    public PageDisplayItem Search(String searchStr,long currPage) {

       long totalRecord = 0;

       Long userCount = (Long) getSqlMapClientTemplate().queryForObject(

              "com.ib.message.model.Message.searchCount",searchStr);

       if (userCount != null)

           totalRecord = userCount;

       PageDisplayItem page = PageUtils.getPageDisplayItem(totalRecord,

              currPage, 5);

       Map map = new HashMap();

       map.put("recordBegin", new Long(page.getRecordBegin() - 1));

       map.put("recordLimit", new Long(page.getRecordLimit()));

       map.put("searchStr", searchStr);

       page.setList(getSqlMapClientTemplate().queryForList(

              "com.ib.message.model.Message.search", map));

       return page;

    }

 

下一层:util是一个实体工具类,是一些辅助的类

如分页的类,字符转换类等

 

再有一层:Action

例如:MessageAction

    public MessageManager messageManager;

    public void setMessageManager(MessageManager messageManager) {

       this.messageManager = messageManager;}

    public ActionForward save(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

       Message message = new Message();

       message

              .setName(WebRequestUtils

                     .getStringParameterTrim(request, "name"));

       message.setTitle(WebRequestUtils.getStringParameterTrim(request,

              "title"));

       message.setEmail(WebRequestUtils.getStringParameterTrim(request,

              "email"));

       message.setcontent(WebRequestUtils.getStringParameterTrim(request,

              "content"));

       message.setTime(new Date());

       // System.out.println(message.getTime());

       messageManager.insert(message);

       // System.out.println("has insert one message!");

       return mapping.findForward(SUCCESS);

    }

上面是insert操作的action部分,实现插入记录到Message表中,根据插入情况,跳转到相对应的网页中.

Form层,提拱给action使用.

getter/setter,还有reset,validate函数方法.

 

 

第四步:重要的配置文件

Struts-config.xml

添加Struts框架对spring的支持:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>

 

  保证提交的内容能被Spring截获

  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">

  </controller>

 

 

 

 

添加validation验证框架:

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">

       <set-property property="pathnames"

                    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

    </plug-in>

对应到:

这两个文件

 

加载一些资源包:

    <message-resources parameter="i18n/messages"/>

 

Spring-config-page.xml配置文件

配置一些bean,即可对应以前struts-config里面的一些action.

例如:

 <bean name="/page/message" class="com.ib.message.web.action.MessageAction" />

 

而在struts-config_page.xml文件中配置:

<action path="/page/message" name="messageForm" scope="request"

       parameter="method" validate="false">

 

<forward name="list" path="/WEB-INF/pages/page/messageList.jsp" />

<forward name="edit" path="/WEB-INF/pages/page/messageForm.jsp" />

<forward name="searchList" path="/WEB-INF/pages/page/messageSearchList.jsp" />

 <forward name="success" path="/page/message.do" redirect="true" />

</action>

注意:这个action标记可以配置在strut-config.xml文件中,但为安全和将一些配置属性放置到其他的配置文件考虑.而且这个action下面有四个跳转指令

 

如何让spring框架认识到Spring_config_page.xml.

有办法:action-servlet.xml文件中:

<!-- 按模块导入Spring Action Config-->

    <import resource="modules/spring-config-admin.xml" />

    <import resource="modules/spring-config-page.xml" />

 

 

还有如何让Struts Action认识到struts-config.xml,struts-config-admin.xml,struts-config-page.xml

方法:

web.xml文件中进行以下配置:

 

 

 

<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,

/WEB-INF/modules/struts-config-admin.xml,

/WEB-INF/modules/struts-config-page.xml

</param-value>

       </init-param> 

      ………………………………………………………………………..

</servlet>

 

web.xml中还有一些重要的设置:struts-config.xml中相对应

    <!-- Spring ApplicationContext配置文件的路径。可使用通配符,多个路径用逗号分隔。

      此参数用于后面的“Spring-Context loader” --> 

    <context-param>

        <param-name>contextConfigLocation</param-name> 

        <param-value>classpath*:spring/*.xml</param-value>

    </context-param>

 

    <!--Spring ApplicationContext 载入 --> 

 <listener>    

   <listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener> 

 

 

Error Page定义

    <error-page>

        <error-code>500</error-code> 

        <location>/commons/error.jsp</location>

    </error-page> 

   

    <error-page>

        <error-code>404</error-code> 

        <location>/commons/404.jsp</location>

    </error-page> 

   

    <error-page>

        <error-code>403</error-code> 

        <location>/commons/403.jsp</location>

    </error-page>  

 

     session超时定义,单位为分钟

    <session-config>

        <session-timeout>10</session-timeout>

    </session-config>

中文乱码问题的解决:

  <!-- 著名 Character Encoding filter --> 

    <filter>

        <filter-name>encodingFilter</filter-name> 

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 

        <init-param>

            <param-name>encoding</param-name> 

            <param-value>UTF-8</param-value>

        </init-param>

    </filter> 

    <filter-mapping>

        <filter-name>encodingFilter</filter-name> 

        <url-pattern>*.do</url-pattern>

    </filter-mapping> 

    <filter-mapping>

        <filter-name>encodingFilter</filter-name> 

        <url-pattern>*.jsp</url-pattern>

    </filter-mapping> 

 

 

<!-- Spring 刷新Introspector防止内存泄露 --> 

<listener>

 <listener-class>

org.springframework.web.util.IntrospectorCleanupListener

</listener-class>

 </listener>

 

 

 

 

 

 

第五步:jsp,css,js等文件

注意这里的jsp不可以直接被访问到

要通过相对路径访问,如http://localhost:8080/Message/page/message.do

分析这个URL,注意,Message为这个项目的根路径名:

/page/messagepath="/page/message"即为path的属性值.

默认情况下为:


<forward name="success" path="/page/message.do" redirect="true" />

跳转到这个地方.

 

Default.css配置一些样式表,为jsp文件提拱一些友好的风格

 

第六步:日志文件----log4j.properties

 

# This is the configuring for logging displayed in the Application Server

log4j.rootCategory=INFO, stdout,logfile

 

#stdout configure

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n

 

#logfile configure

log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.logfile.File=../logs/@project.name@.log

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n

 

# Control logging for other open source packages

# Changing the log level to DEBUG when debug

log4j.logger.org.springframework=WARN

# Changing the log level to DEBUG will display SQL Hibernate generated

log4j.logger.org.hibernate=WARN

log4j.logger.org.hibernate.SQL=ERROR

log4j.logger.org.hibernate.cache=ERROR

log4j.logger.net.sf.ehcache=ERROR

…………………………..

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值