SSH2自学逻辑

来源:互联网 发布:python基础书籍下载 编辑:程序博客网 时间:2024/06/03 16:19

1、创建java web程序,加入jar文件

2、编写bean.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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

</beans>

3、编写配置

<context:annotation-config/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
   <property name="username" value="root"/>
   <property name="password" value="123456"/>
    <!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
 </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
   <list>
     <value>cn/itcast/bean/Person.hbm.xml</value>
   </list>
</property>
    <property name="hibernateProperties">
   <value>
       hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
       hibernate.hbm2ddl.auto=update
       hibernate.show_sql=false
       hibernate.format_sql=false
       hibernate.cache.use_second_level_cache=true
               hibernate.cache.use_query_cache=false
           hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
     </value>
    </property>
 </bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>  ---事务管理

4、编写普通类Person类。

public class Person {
private Integer id;
private String name;

public Person(){}

public Person(String name) {
this.name = name;
}

       .....省略get,set方法。

5、植入对应的Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
    <class name="Person" table="person">
    <cache usage="read-write" region="cn.itcast.bean.Person"/>
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" length="10" not-null="true"/>
    </class>
</hibernate-mapping>

6、编写对应的二级缓存文件ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    defaultCache节点为缺省的缓存策略
     maxElementsInMemory 内存中最大允许存在的对象数量
     eternal 设置缓存中的对象是否永远不过期
     overflowToDisk 把溢出的对象存放到硬盘上
     timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
     timeToLiveSeconds 指定缓存对象总的存活时间
     diskPersistent 当jvm结束是是否持久化对象
     diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
 -->
<ehcache>
    <diskStore path="D:\cache"/>
    <defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="180"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="60"/>
<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
    overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
</ehcache>

7、编写struts2的配置文件,载入person对象。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
     <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.objectFactory" value="spring" />
    
  <package name="person" namespace="/person" extends="struts-default">
  <global-results>
  <result name="message">/WEB-INF/page/message.jsp</result>
  </global-results>
<action name="action_*" class="personList" method="{1}">
<result name="list">/WEB-INF/page/personlist.jsp</result>
<result name="add">/WEB-INF/page/addperson.jsp</result>
</action>
    </package>
</struts>

8、编写业务类

public interface PersonService {
public void save(Person person);
public void update(Person person);
public Person getPerson(Integer personid);
public void delete(Integer personid);
public List<Person> getPersons();
}

9、编写实现类

package cn.itcast.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

@Transactional
public class PersonServiceBean implements PersonService {
@Resource private SessionFactory sessionFactory;

public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}

public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer personid){
return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
}

public void delete(Integer personid){
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, personid));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}

10、编写web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
   </filter-mapping>
<filter>
       <filter-name>OpenSessionInViewFilter</filter-name>
       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>OpenSessionInViewFilter</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

10、编写PersonAction类

package cn.itcast.web;
import java.util.List;
import javax.annotation.Resource;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

public class PersonAction {
@Resource PersonService personService;
private String message;
private List<Person> persons;
private Person person;

public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
/**
* 人员列表显示
*/
public String list(){
this.persons = personService.getPersons();
return "list";
}
/**
* 人员添加界面
*/
public String addUI(){
return "add";
}
/**
* 人员添加
*/
public String add(){
this.personService.save(this.person);
this.message="添加成功";
return "message";
}
public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

11、编写personlist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>人员列表</title>
  </head>
  
  <body>
   <s:iterator value="persons">
    id=<s:property value="id"/>,name=<s:property value="name"/><br>
   </s:iterator>
  </body>
</html>

12、编写message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My message page</title>
  </head>
  
  <body>
    <s:property value="message"/> <br>
  </body>
</html>

13、编写addperson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>人员添加</title>
  </head>
  
  <body>
    <s:form action="action_add" namespace="/person">
    名称:<s:textfield name="person.name"/>
    <input type="submit" value="提交"/>
    </s:form>
  </body>
</html>















原创粉丝点击