SSH2框架搭建示例
来源:互联网 发布:UNetbootin ubuntu 编辑:程序博客网 时间:2024/05/16 10:19
SSH2
本文只是入门搭建,要想深入可自行摸索。
首先我们建立一个工程,所需jar包如下:SSH2所需jar打包下载
jar包注意不要重复了。
下面开始配置:
- web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>MySystem</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> <!-- 配置资源 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <!-- 配置spring --> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置CharacterEncoding,设置字符集 --> <filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Struts2配置 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><init-param><param-name>config</param-name><param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value></init-param></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
- 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" 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- spring --><context:annotation-config /><!-- sessionFactory --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/mysql"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!-- Hibernate--><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><property name="mappingLocations"><list><value>hibernate.cfg.xml</value></list></property></bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="del*" propagation="REQUIRED" no-rollback-for="MyException"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="daoMethods" expression="execution(* com.wanghaifeng.MySystem.dao.*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="daoMethods"/> </aop:config> <!-- 这里名称要与structs里的一致 --> <bean id="userAction" class="com.wanghaifeng.MySystem.action.UserAction" scope="prototype"></bean></beans>
- structs.xml
<?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.3.dtd"><struts><!-- 告诉struts2,action的创建交给spring --><constant name="struts.ObjectFactory" value="spring"></constant> <!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 --> <constant name="struts.action.extension" value=","></constant> <package name="struts" namespace="/" extends="struts-default"> <!-- 配置拦截器 --> <interceptors> <interceptor name="myInterceptor" class="myInterceptor"></interceptor> <interceptor-stack name="myDefult"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- name是jsp请求 --> <action name="getUser" class="userAction" method="errUser"> <result name="success">/success.jsp</result> <result name="error" type="redirect">/error.jsp</result> </action> </package> </struts>
- hibernate.cfg.xml
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql</property> <property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="com.wanghaifeng.MySystem.entity.User"/> </session-factory></hibernate-configuration>
- action
package com.wanghaifeng.MySystem.action;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.opensymphony.xwork2.ActionSupport;import com.wanghaifeng.MySystem.entity.User;public class UserAction extends ActionSupport{public String getUser() {return "success";}private static SessionFactory factory;public String errUser() {String hqlString = " from User"; Configuration cfg=new Configuration().configure(); //建立SessionFactory factory=cfg.buildSessionFactory(); //获取Session Session session=factory.openSession(); List<User> query = session.createQuery(hqlString).list(); User user = (User)query.get(0); System.out.println("年龄:"+user.getAge()+"姓名:"+user.getUserName()+"号码:"+user.getUserId());return "error";}}
输出
Hibernate: select user0_.USERNAME as USERNAME0_, user0_.AGE as AGE0_, user0_.PASSWORD as PASSWORD0_, user0_.USERID as USERID0_ from T_USER user0_
年龄:22姓名:王海峰号码:1
0 0
- SSH2框架搭建示例
- ssh2框架搭建
- SSH2框架搭建
- ssh2框架搭建
- SSH2框架搭建
- SSH2框架搭建--详细
- SSH2框架搭建
- SSH2框架搭建
- ssh2框架搭建
- ssh2框架搭建
- SSH2框架搭建
- SSH2框架搭建
- SSH2框架搭建
- MyEclipse8.5搭建ssh2框架
- ssh2框架搭建及代码
- ssh2框架搭建及代码
- SSH2框架的搭建条件
- SSH2框架搭建 和 配置文件详解
- Delphi XE8,C++ Builder XE8,RAD Studio XE8 精简版 EMBARCADERO DELPHI XE8 LITE V11.X
- 浅谈js之闭包
- 西风的数据结构教程(2)——队列
- webbuilder安装记录
- ios学习笔记 (4)数据持久化
- SSH2框架搭建示例
- Html5系列(二十七)在html5中通过JavaScript显示隐藏网页内容
- 对数据操作后,设置了adapter.notifyDataSetChanged() ListView数据不更新
- setTimeout和setInterval的区别
- Android开发中网络请求的压缩 ── GZip的使用
- 初学html5
- Html5系列(二十六) 布局标签
- Html5系列(二十四)款赏心悦目的 HTML5/CSS3 特效
- <linux><ubuntu>基础工作