SSH框架搭建demo
1、新建Java Web工程
2、添加Struts2.1框架支持
去除冲突包:antlr-2.7.2.jar
一般项目加上这三个包足够,后期可以视项目需求增加支持包:
2.1配置web.xml文件
增加struts2的配置,增加容器对struts2的初始化。增加代码如下:这些配置代码对于struts2是不变的,直接复制到web.xml即可。
- <!-- struts2过滤器,对任何请求都添加这个过滤器 -->
- <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>
2.2配置struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- </struts>
3、添加Spring框架支持
个支持包
效果图:
3.1配置web.xml文件
初始化加载spring资源配置文件
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/applicationContext.xml
- </param-value>
- </context-param>
- <!-- 对Spring容器进行实例化-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
- </listener>
4、配置数据源
此处用oracle数据库作为数据源:
- create user ssh identified by 1234;
- grant dba to ssh with admin option;
- create table people (
- id varchar2(32) primary key,
- username varchar2(32),
- password varchar2(32)
- );
- insert into PEOPLE (ID, USERNAME, PASSWORD) values ('1', 'John', '1234');
- insert into PEOPLE (ID, USERNAME, PASSWORD) values ('2', 'Tom', '1234');
驱动包直接用oracle自带的jdbc驱动包即可,路径如上图所示。
5、添加Hibernate框架支持
删除冲突包:asm.jar和cglib-2.1.3.jar
切换到MyEclipse Database Explorer视图:
选择我们建立的链接SSH,点开,找到对应的table,people,右键
6、代码设计
为了方便管理代码,先建立四个package,如下图:
在com.ssh.dao下建立一个接口,IPeopleDAO.java
- package com.ssh.dao;
- import java.util.List;
- import com.ssh.people.People;
- public interface IPeopleDAO {
- public People findById(String id);
- public List findByUsername(Object username);
- public void save(People people);
- }
把PeopleDAO拖到com.ssh.dao.impl下
PeopleDAO.java里要加implements IPeopleDAO
修改后:
在com.ssh.service下建立IPeopleService.java
- package com.ssh.service;
- import com.ssh.people.People;
- public interface IPeopleService {
- public People getPeopleById(String id);
- public People getPeopleByUsername(String username);
- public void addPeople(People people);
- }
在com.ssh.service.impl下建立PeopleService.java
- package com.ssh.service.impl;
- import java.util.List;
- import com.ssh.dao.IPeopleDAO;
- import com.ssh.people.People;
- import com.ssh.service.IPeopleService;
- public class PeopleService implements IPeopleService{
- private IPeopleDAO peopleDAO;
- public People getPeopleById(String id) {
- return peopleDAO.findById(id);
- }
- public People getPeopleByUsername(String username) {
- List list = peopleDAO.findByUsername(username);
- if(list.size() == 0){
- return null;
- }else{
- return (People)list.get(0);
- }
- }
- public void addPeople(People people) {
- peopleDAO.save(people);
- }
- public IPeopleDAO getPeopleDAO() {
- return peopleDAO;
- }
- public void setPeopleDAO(IPeopleDAO peopleDAO) {
- this.peopleDAO = peopleDAO;
- }
- }
效果图:
接着要在applicationContext.xml中添加一个bean的信息
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
整个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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="oracle.jdbc.OracleDriver">
- </property>
- <property name="url"
- value="jdbc:oracle:thin:@localhost:1521/orcl">
- </property>
- <property name="username" value="ssh"></property>
- <property name="password" value="1234"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ssh/people/People.hbm.xml</value></list>
- </property>
- </bean>
- <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
- </beans>
创建处理action的文件,新建com.ssh.action包,新建LoginAction.java文件
- package com.ssh.action;
- import com.opensymphony.xwork2.ActionSupport;
- import com.ssh.people.People;
- import com.ssh.service.IPeopleService;
- public class LoginAction extends ActionSupport{
- private String username;
- private String password;
- private IPeopleService peopleService;
- 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 IPeopleService getPeopleService() {
- return peopleService;
- }
- public void setPeopleService(IPeopleService peopleService) {
- this.peopleService = peopleService;
- }
- public String execute(){
- People people = peopleService.getPeopleByUsername(username);
- if(people == null) return "error";
- if(people.getPassword().equals(password))
- return "success";
- else return "error";
- }
- public void validate(){
- if(username == null || username.length() == 0){
- super.addActionError("用户名不能为空");
- }
- if(password == null || password.length() == 0){
- super.addActionError("密码不能为空");
- }
- }
- }
配置applicationContext.xml文件:
- <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
- <property name="peopleService" ref="peopleService"></property>
- </bean>
配置struts.xml文件:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="com" extends="struts-default" namespace="/">
- <action name="login" class="loginAction">
- <result name="success">/index.jsp</result>
- <result name="input">/login.jsp</result>
- <result name="error">/login.jsp</result>
- </action>
- </package>
- </struts>
新增login.jsp文件:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <%
- 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>登录页面</title>
- </head>
- <body>
- <s:form name="form" action="login.action" method="post">
- <s:textfield name="username" label="username"></s:textfield>
- <s:password name="password" label="password"></s:password>
- <s:submit label="submit"></s:submit>
- </s:form>
- <s:actionerror/>
- </body>
- </html>
6.1完整的struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="com" extends="struts-default" namespace="/">
- <action name="login" class="loginAction">
- <result name="success">/index.jsp</result>
- <result name="input">/login.jsp</result>
- <result name="error">/login.jsp</result>
- </action>
- </package>
- </struts>
6.2完整的web.xml文件
- <?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">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <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>
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/applicationContext.xml
- </param-value>
- </context-param>
- <!-- 对Spring容器进行实例化-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
- </listener>
- </web-app>
6.3完整的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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="oracle.jdbc.OracleDriver">
- </property>
- <property name="url"
- value="jdbc:oracle:thin:@localhost:1521/orcl">
- </property>
- <property name="username" value="ssh"></property>
- <property name="password" value="1234"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ssh/people/People.hbm.xml</value></list>
- </property>
- </bean>
- <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
- <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
- <property name="peopleService" ref="peopleService"></property>
- </bean>
- </beans>
7、部署运行
7.1Tomcat服务器
7.2去除antlr-2.7.2.jar冲突包
apache-tomcat-7.0.54\webapps\SSH_Demo\WEB-INF\lib目录下
7.3整理jar包
把apache-tomcat-7.0.54\webapps\SSH_Demo\WEB-INF\lib目录下的jar包全部复制出来,这个就是SSH框架所需的jar包。
然后把jar.zip中的jar包导入即可。
7.4运行访问
8、常见问题
1) 关于 myeclipse 里面没有 add struts capabilities 问题解决方法
高版本的 myeclipse 里面的 add struts capabilities 在如下路径中:(当你加载过一次之后,这个 add struts capabilities就不会出现。
解决方法:把 .project 文件的关于struts的配置删除掉即可
附件源码
下载地址:https://gitee.com/KingXin666/SSH_Demo
SSH框架搭建demo的更多相关文章
- 【Java EE 学习 69 中】【数据采集系统第一天】【SSH框架搭建】
经过23天的艰苦斗争,终于搞定了数据采集系统~徐培成老师很厉害啊,明明只是用了10天就搞定的项目我却做了23天,还是模仿的...呵呵,算了,总之最后总算是完成了,现在该好好整理该项目了. 第一天的内容 ...
- ssh框架搭建的基本步骤(以及各部分作用)
ssh框架搭建的基本步骤(以及各部分作用) 一.首先,明确spring,struts,hibernate在环境中各自的作用. struts: 用来响应用户的action,对应到相应的类进行 ...
- 转 SSH框架搭建详细图文教程
原址:http://blog.sina.com.cn/s/blog_a6a6b3cd01017c57.html 什么是SSH? SSH对应 struts spring hibernatestruts ...
- SSH框架搭建 详细图文教程
转载请标明原文地址 一.什么是SSH? SSH是JavaEE中三种框架(Struts+Spring+Hibernate)的集成框架,是目前比较流行的一种Java Web开源框架. SSH主要用于Jav ...
- SSH框架搭建详细图文教程(转)
这篇文章看的我醍醐灌顶的感觉,比之前本科时候学习的SSH架构 要清晰数倍 非常感觉这篇博主的文章 文章链接为:http://blog.sina.com.cn/s/blog_a6a6b3cd01017 ...
- SSH框架搭建步骤总结以及Hibernate二级缓存,查询缓存
二级缓存.查询缓存 一级缓存: 默认启动,生命周期是和session同步的,session独享 二级缓存: 需要加载配置信息,生命周期是和应用服务器同步,session共享 1:在hibernate. ...
- SSH框架搭建详细图文教程
转载请标明原文地址:http://www.cnblogs.com/zhangyukof/p/6762554.html 一.什么是SSH? SSH是JavaEE中三种框架(Struts+Spring+H ...
- SSM、SSH框架搭建,面试点总结
文章目录 1.SSM如何搭建:三个框架的搭建: 2.SSM系统架构 3.SSM整合步骤 4.Spring,Spring MVC,MyBatis,Hibernate个人总结 5.面试资源 关于SSM.S ...
- SSH框架搭建最终版【测试、log4j、baseDao】
最详细搭建SSH框架环境 本博文主要是讲解如何搭建一个比较规范的SSH开发环境,以及对它测试[在前面的搭建中,只是整合了SSH框架,能够使用SSH实现功能],而这次是相对规范的. 导入开发包 在Str ...
随机推荐
- Java学习笔记10(面对对象:构造方法)
在开发中经常需要在创建初始化对象时候明确对象的属性值, 比如Person对象创建的时候就给Person的属性name,age赋值, 这里就要用到构造方法: 构造方法是类的一种特殊方法,它的特殊性体现在 ...
- Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri
文章目录 1. Jetty 的切换 2. Undertow的使用 Spring Boot 可选择内嵌 Tomcat.Jetty 和 Undertow,因此我们不需要以 war 包形式部署项目.< ...
- gl_PointSize偶数像素表现精准,基数会模糊化。
- pip install GitHub package
/********************************************************************************* * pip install Git ...
- Wifi设备接入流程
这是硬件平台下文档结构图,详细了解戳这里具体了解这里. 什么意思呢?要想实现控制wifi设备,基本流程是: 咱自己的wifi设备----------自己的服务器(指图中的厂商服务器)-------微信 ...
- 测试那些事儿—浅谈TCP/IP协议
TCP/IP协议是一系列网络协议的总和,是构成网络通信的核心骨架. TCP/IP的工作原理通俗的讲就是一个主机的数据要经过哪些过程才能发送到对方的主机上. TCP/IP协议采用四层结构,分别为应用层, ...
- jmeter定时器
一.定时器的作用 1.定时器是在每个sampler(采样器)之前执行的,而不是之后(无论定时器位置在sampler之前还是下面): 2.当执行一个sampler之前时,所有当前作用域内的定时器都会被执 ...
- [Codeforces Round #526 (Div. 2)]
https://codeforces.com/contest/1084 A题 数据量很小,枚举就行 #include<iostream> #include<cstdio> #i ...
- 【HDOJ4612】【双连通分量缩点+找树的直径】
http://acm.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Others) Me ...
- Thinkphp,Jquery,Ajax异步发布
1.在提交表单的HTML页面的<head>中定义一个变量供Jquery使用 <script type='text/javascript'>var handleUrl='< ...