SSH环境搭建步骤解析

一、建立Java web project:AngelSSH

二、引入jar包,必要清单如下

2.1,Struts2

commons-fileupload  文件上传组件

commons-io   io包

freemarker   一个基于模板生成文本输出的通用工具

ognl   对象图导航语言

struts2-core   struts2核心包

xwork-core    xwork核心包

struts2-spring-plugin  struts和spring的整合包

2.2,Spring

spring   spring核心包

aspectjrt、aspectjweaver  aop支持

cglib-nodep   aop支持(两种形式,cglib代理为子类代理,另一种是接口代理)

commons-logging   日志支持,抽象级别高于log4j

2.3,Hibernate

hibernate   hibernate核心包

antlr   执行sql查询

commons-collections   为Java标准的Collections API提供补充

dom4j   xml配置和映射解释器

javassist   扩展Java类和实现

jta   事务管理

slf4j   简单日志门面,服务于其他日志系统

hibernate-jpa   定义Java持久性

c3p0   连接池

mysql-connection-java   mysql(数据库)连接驱动

2.4,Junit

IDE自带jar包

三、建立配置文件

3.1,关于Struts2

3.1.1,添加Struts2的配置文件(src):struts.xml

<span style="font-family:KaiTi_GB2312;font-size:18px;">		<?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>
<!-- 配置开发环境,当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/> <!-- 会提供更加友好的提示信息 -->
<constant name="struts.devModel" value="true"/> <!-- 可使用多个include标签 <include file="struts_user.xml"/>--> <!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="struts2" extends="struts-default"> </package> </struts> </span>

3.1.2,在web.xml里面配置Struts2的通用拦截器(web-inf)

<span style="font-family:KaiTi_GB2312;font-size:18px;"><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></span>

3.2,关于hibernate

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory name="foo"> <!-- 数据库连接信息 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/angeloa</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Angel0626</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 其他配置 -->
<property name="hibernate.show_sql">true</property> <!-- 导入映射文件 --> </session-factory>
</hibernate-configuration></span>

3.3,关于Spring

首先,建立一个文件:applicationContext-common.xml,配置数据库事务等方面的东西

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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</value>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean> <!-- 配置事务的传播特性(配置Advice) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="java.lang.Exception" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 那些类那些方法使用事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* cn.Angel.ssh.service.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>
</beans></span>

然后,建立一个主文件,配置spring:applicationContext.xml

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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">
<!-- 启用AspectJ对Annotation的支持 -->
<aop:aspectj-autoproxy /> <!-- 强制使用CGLIB代理 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true" /> --> <!--配置事务 -->
<import resource="applicationContext-common.xml" /> </beans></span>

最后,在web.xml文件中配置spring监听:

<span style="font-family:KaiTi_GB2312;font-size:18px;">	<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></span>

3.4,关于日志

引入log4j.properties

<span style="font-family:KaiTi_GB2312;font-size:18px;">log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=<span style="color:#ff0000;">warn</span>, stdout log4j.logger.org.hibernate=info log4j.logger.org.hibernate.type=info log4j.logger.org.hibernate.tool.hbm2ddl=debug
</span>

注意红色标记的部分:配置合适的日志提醒等级,当前配置的为warn,也就是会打印出warn,以及warn以上的错误信息。

四、完成框架的剩余部分

4.1,实体

4.1.1,编写实体类User.java

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.domain;

public class User {
private String id;
private String userName;
private String password;
private String userCode;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
}
</span>

4.1.2,编写实体的映射文件user.hbm.xml

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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>
<class name="cn.Angel.ssh.domain.User" table="t_user">
<id name="id" length="50">
<generator class="uuid" />
</id>
<property name="userCode" length="18" />
<property name="userName" length="10" />
<property name="password" length="16" />
</class>
</hibernate-mapping></span>

4.1.3,在Hibernate配置文件中,添加实体映射文件的引入

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 导入映射文件 -->
<mapping resource="cn/Angel/ssh/domain/User.hbm.xml" /></span>

4.1.4,编写ExportDB类

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.domain;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDb {
public static void main(String[] args) {
// 默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}</span>

执行方法,根据实体生成表

4.2,建立Dao层

4.2.1,建立IUser接口

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.dao;

import cn.Angel.ssh.domain.User;

public interface IUser {

	boolean login(User user);
}
</span>

4.2.2,建立UserImpl类,实现接口IUser

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.dao;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.Angel.ssh.domain.User; public class UserImpl extends HibernateDaoSupport implements IUser { @SuppressWarnings("unchecked")
public boolean login(User user) {
List<User> list = (List<User>) getHibernateTemplate().find(
" from User where userCode='" + user.getUserCode()
+ "' and password='" + user.getPassword() + "' ");
if (list.size() > 0) {
return true;
} else {
return false;
}
}
}</span>

4.2.3,在applicationContext.xml中配置Dao层的注入

注:可以仿照applicationContext-common文件一样为dao层建立一个xml文件,然后用import引入

<span style="font-family:KaiTi_GB2312;font-size:18px;">	<!-- 注入Dao层 -->
<bean id="userDao" class="cn.Angel.ssh.dao.UserImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean></span>

4.3,建立Service层

4.3.1,建立UserService接口

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.service;

import cn.Angel.ssh.domain.User;

public interface UserService {

	boolean login(User user);
}</span>

4.3.2,建立UserServiceImpl类,实现UserService接口

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.service;

import cn.Angel.ssh.dao.UserImpl;
import cn.Angel.ssh.domain.User; public class UserServiceImpl implements UserService{ //注入dao层
private UserImpl userDao; public UserImpl getUserDao() {
return userDao;
} public void setUserDao(UserImpl userDao) {
this.userDao = userDao;
} public boolean login(User user) {
return userDao.login(user);
} }</span>

4.3.3,在applicationContext.xml文件中配置service层注入

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 注入Service层 -->
<bean id="userService" class="cn.Angel.ssh.service.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean></span>

4.4,建立web层

4.4.1,建立UserAction.java类,控制页面的访问

<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.Angel.ssh.web;

import cn.Angel.ssh.domain.User;
import cn.Angel.ssh.service.UserService; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); public User getModel() {
return user;
} private UserService userService; public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} public String login() {
boolean flag = userService.login(user);
if (flag == true) {
return "loginSuccess";
} else {
return "loginFail";
}
}
}</span>

4.4.2,在applicationContext.xml文件中配置action注入

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 用户模块的Action -->
<bean id="userAction" class="cn.Angel.ssh.web.UserAction" scope="prototype" >
<property name="userService" ref="userService" />
</bean></span>

4.4.3,在Struts.xml文件中,配置userAction的跳转页面

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="struts2" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<!-- <result>标签的那么属性,如果不配置,那么缺省值为success -->
<result name="loginSuccess">/login_success.jsp</result>
<result name="loginFail">/login_error.jsp</result>
</action>
</package></span>

4.5,建立页面

4.5.1,login.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><body>
<form action="user_login.action">
用户代码:<input type="text" name="userCode"> 用户密码:<input
type="password" name="password"> <input type="submit"
value="登录">
</form>
</body></span>

4.5.2,login_success.jsp、login_error.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><body>
登录成功!<br>
</body></span>

附:程序结构图

SSH环境搭建步骤解析

4.6,将程序添加至tomcat或者其他服务器,启动

访问:localhost:8080/AngelSSH/login.jsp

五、总结

按照以上的步骤来,最终一定能实现SSH的简单搭建。本篇博客介绍的是注入的方法,还可以使用注解。引入了各种jar包,也可以通过maven仓库来解决。接下来会继续总结SSH框架搭建过程中的一些问题。呼呼,这是我第十一遍搭,在第9次之后,终于能搭一遍成一遍了,做个总结,供自己参考!

上一篇:Unity3D判断触摸方向


下一篇:爬虫-day01-基础知识