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 ...
随机推荐
- DOM树中节点与节点之间的关系图
获取子代元素节点:children 获取所有子代元素节点数:**.childElementCount 或者 **.children.length
- 【Python】多进程-4
#练习:用event事件控制进程执行顺序,下面例子中,主进程main函数在创建了子进程之后,依然会往下执行,所以会出现主进程先打印出来的情况 import multiprocessing import ...
- 【Python】xml 解析
1. XML:指可扩展标记语言,是一种标记语言,用于存储数据和传输数据,但没有像HTML那样具有预定义标签,需要程序猿自定义标签 2. XML的解析:读取XML数据结构中的某些信息,比如读取书的属性 ...
- LazyMan的实现
题目如下: 实现一个LazyMan,可以按照以下方式调用: LazyMan(“Hank”)输出: Hi! This is Hank! LazyMan(“Hank”).sleep(10).eat(“di ...
- 原生WebGL绘制3个点
<html> <body> <canvas width = "300" height = "300" id = "my_ ...
- c# 休眠后 定时唤醒
源码见附件,执行唤醒功能前需要先对电源进行设置如下图: 源码
- Gym101485: NWERC 2015(队内第6次训练)
A .Assigning Workstations 题意:给定N个人的工作时间和工作时长,我们可以假设有无数台工作机器,如果一台机器超过M时间未使用就会关闭,那么我们怎么安排机器的使用,使得需要开启机 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
- Elasticsearch Docker环境下安装
Elasticsearch Docker环境下安装 Daemon镜像配置的是https://registry.docker-cn.com Linux:vi /etc/docker/daemon.jso ...
- C++学习(十)(C语言部分)之 分支语句
#include<stdio.h> // std 标准 io ----> input output 输入 输出 printf scanf getchar ...... #i ...