6月13日 SSH 周三

时间:2022-03-31 14:10:43
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>
    </filter-mapping>
    <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>
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

  </head>

  <body>
    <table border="1px">
        <tr>
            <th>id</th>
            <th>age</th>
            <th>color</th>
            <th>name</th>
        </tr>
        <c:forEach var="p" items="${pigList}">
            <tr>
                <th>${p.id}</th>
                <th>${p.age}</th>
                <th>${p.color}</th>
                <th>${p.name}</th>
            </tr>
        </c:forEach>

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

<struts>
    <constant name="struts.devMode" value="true"></constant>

    <package name="abc" extends="struts-default">
        <action name="p_*" class="com.action.Action" method="{1}">
            <result name="list">list.jsp</result>
        </action>
    </package>
</struts>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///ssh01</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <mapping resource="com/dto/Dto.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
<?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: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/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">
    <!-- 创建sessionfactory对象 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <bean id="dao" class="com.dao.PigDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="service" class="com.service.PigService">
        <property name="dao" ref="dao"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean><!--事务处理器 -->
    <!-- 通知(增强处理):对事务的管理方案 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" timeout="-1" rollback-for="java.lang.Exception"/>
            <tx:method name="update*"/>
            <tx:method name="del*"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.service.*.*(..))"/>
    </aop:config>
</beans>
<?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 default-lazy="false">
    <class name="com.dto.Pig" table="t_pig">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="age"/>
        <property name="color"/>
        <property name="name"/>
    </class>
</hibernate-mapping>
package com.dto;

public class Pig {
    private Integer id;
    private Integer age;
    private String color;
    private String name;
    public Pig() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Pig(Integer id, Integer age, String color, String name) {
        super();
        this.id = id;
        this.age = age;
        this.color = color;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Pig [id=" + id + ", age=" + age + ", color=" + color
                + ", name=" + name + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


}
package com.action;

import java.util.List;

import com.dto.Pig;
import com.opensymphony.xwork2.ActionSupport;
import com.service.PigService;

public class Action extends ActionSupport{
    private PigService service;
    private List<Pig> pigList;
    private Pig pig;

    public String list(){
        pigList = service.findPigList();
        System.out.println(pigList);
        return "list";
    }

    public List<Pig> getPigList() {
        return pigList;
    }

    public void setPigList(List<Pig> pigList) {
        this.pigList = pigList;
    }

    public Pig getPig() {
        return pig;
    }

    public void setPig(Pig pig) {
        this.pig = pig;
    }

    public PigService getService() {
        return service;
    }

    public void setService(PigService service) {
        this.service = service;
    }


}
package com.service;

import java.util.List;

import com.dao.PigDao;
import com.dto.Pig;

public class PigService {
    private PigDao dao;

    public List<Pig> findPigList() {
        // TODO Auto-generated method stub
        return dao.findPigList();
    }

    public PigDao getDao() {
        return dao;
    }

    public void setDao(PigDao dao) {
        this.dao = dao;
    }


}
package com.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dto.Pig;

public class PigDao extends HibernateDaoSupport{

    public List<Pig> findPigList() {
        // TODO Auto-generated method stub
        return getHibernateTemplate().find("from Pig");
    }

}

如果发出声音是危险的,那就保持沉默;如果自觉无力发光,那就别去照亮别人。 但是,不要习惯了黑暗就为黑暗辩护;不要为自己的苟且而得意洋洋;不要嘲讽那些比自己更勇敢、更有热量的人们。 可以卑微如尘土,不可扭曲如蛆虫。