SSH

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssh_test</display-name>
  <welcome-file-list>
    
    <welcome-file>index.jsp</welcome-file>
    
  </welcome-file-list>
  <!-- struts核心过滤器 -->
  <filter>
  <filter-name>struts</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 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>

</web-app>


action

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{


Employee employee = new Employee();

@Override
public Employee getModel() {
// TODO Auto-generated method stub
return employee;
}

//使用spring传入EmployeeService实例
EmployeeService employeeService;

public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}


public String save(){
System.out.println("action执行了");
employeeService.saveEmployee(employee);

return "success";

}

service

public class EmployeeService {


//使用spring自动注入EmployeeDao实例
EmployeeDao employeeDao;


public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}




public void saveEmployee(Employee employee) {
// TODO Auto-generated method stub
System.out.println("service执行了");
employeeDao.saveEmployee(employee);
}

}

dao

public class EmployeeDao extends HibernateDaoSupport{


public void saveEmployee(Employee employee) {
// TODO Auto-generated method stub

System.out.println("dao层执行了");
this.getHibernateTemplate().save(employee);
}

}

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-7-2 17:08:46 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.imooc.entity.Employee" table="EMPLOYEE">
        <id name="empId" type="int">
            <column name="EMPID" />
            <generator class="native" />
        </id>
        <property name="empName" type="java.lang.String">
            <column name="EMPNAME" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
    </class>

</hibernate-mapping>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<!-- 创建dao -->
<bean id="employeeDao" class="com.imooc.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建service 自动注入dao -->
<bean id="employeeService" class="com.imooc.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<!-- 创建action -->
<bean id="employeeAction" class="com.imooc.action.EmployeeAction"> 
<property name="employeeService" ref="employeeService"></property>
</bean>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"> </property>
<property name="password" value="1234"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh-test"></property>

</bean>
<!-- 配置hibernate属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dielect">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>
<!-- 加载映射文件 -->
<property name="mappingResources">
<list>
<value>com/imooc/entity/Employee.hbm.xml</value>
</list>
</property>

</bean>


</beans>

struts.xml

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




<struts>
    <package name="rest-showcase" extends="struts-default" namespace="/">
        <action name="employee" class="com.imooc.action.EmployeeAction" method="save">
        <result name="success">/success.jsp</result>
        </action>
    </package>

</struts>

jsp

<body>




<form action="employee" method="post">
用户名:<input type="text" name="empName"/><br>
密码:<input type="password" name="password"/><br>
性别:<input type="text" name="gender"/><br>
<input type="submit" value="提交"/>
</form>
</body>

阅读更多

没有更多推荐了,返回首页