SSH框架搭建demo

1、新建Java Web工程

 

 

2、添加Struts2.1框架支持

 

 

去除冲突包:antlr-2.7.2.jar

 

 

一般项目加上这三个包足够,后期可以视项目需求增加支持包:

2.1配置web.xml文件

增加struts2的配置,增加容器对struts2的初始化。增加代码如下:这些配置代码对于struts2是不变的,直接复制到web.xml即可。

  1. <!-- struts2过滤器,对任何请求都添加这个过滤器 --> 
  2.   <filter>
  3.     <filter-name>struts2</filter-name>
  4. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5.   </filter>
  6.   <filter-mapping>
  7.     <filter-name>struts2</filter-name>
  8.     <url-pattern>/*</url-pattern>
  9.   </filter-mapping>

2.2配置struts.xml文件

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

3、添加Spring框架支持

 

 一般添加这6个支持包

 

 

效果图:

                                      

3.1配置web.xml文件 

初始化加载spring资源配置文件

  1. <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->  
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>
  5. /WEB-INF/applicationContext.xml
  6. </param-value>
  7. </context-param>
  8.   
  9.   <!-- 对Spring容器进行实例化-->  
  10.   <listener>
  11.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12.   </listener>
  13.   <listener>
  14.     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  15.   </listener>

4、配置数据源

此处用oracle数据库作为数据源:

  1. create user ssh identified by 1234;
  2.  
  3. grant dba to ssh with admin option;
  4.  
  5. create table people (
  6. id varchar2(32) primary key,
  7. username varchar2(32),
  8. password varchar2(32)
  9. );
  10.  
  11. insert into PEOPLE (ID, USERNAME, PASSWORD) values ('1', 'John', '1234');
  12. insert into PEOPLE (ID, USERNAME, PASSWORD) values ('2', 'Tom', '1234');

 

                   

 

驱动包直接用oracle自带的jdbc驱动包即可,路径如上图所示。

 

 

 

 

5、添加Hibernate框架支持

 

 

 

删除冲突包:asm.jar和cglib-2.1.3.jar

0%

 

 

 

切换到MyEclipse Database Explorer视图:

选择我们建立的链接SSH,点开,找到对应的table,people,右键

 

 

 

6、代码设计

为了方便管理代码,先建立四个package,如下图:

在com.ssh.dao下建立一个接口,IPeopleDAO.java 

  1. package com.ssh.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import com.ssh.people.People;
  6.  
  7. public interface IPeopleDAO {
  8. public People findById(String id);
  9. public List findByUsername(Object username);
  10. public void save(People people);
  11.  
  12. }

把PeopleDAO拖到com.ssh.dao.impl

PeopleDAO.java里要加implements IPeopleDAO

修改后:

在com.ssh.service下建立IPeopleService.java

  1. package com.ssh.service;
  2.  
  3. import com.ssh.people.People;
  4.  
  5. public interface IPeopleService {
  6. public People getPeopleById(String id);
  7. public People getPeopleByUsername(String username);
  8. public void addPeople(People people);
  9.  
  10. }

在com.ssh.service.impl下建立PeopleService.java

  1. package com.ssh.service.impl;
  2.  
  3. import java.util.List;
  4.  
  5. import com.ssh.dao.IPeopleDAO;
  6. import com.ssh.people.People;
  7. import com.ssh.service.IPeopleService;
  8.  
  9. public class PeopleService implements IPeopleService{
  10.  
  11. private IPeopleDAO peopleDAO;
  12. public People getPeopleById(String id) {
  13. return peopleDAO.findById(id);
  14. }
  15.  
  16. public People getPeopleByUsername(String username) {
  17. List list = peopleDAO.findByUsername(username);
  18. if(list.size() == 0){
  19. return null;
  20. }else{
  21. return (People)list.get(0);
  22. }
  23. }
  24.  
  25. public void addPeople(People people) {
  26. peopleDAO.save(people);
  27. }
  28.  
  29. public IPeopleDAO getPeopleDAO() {
  30. return peopleDAO;
  31. }
  32.  
  33. public void setPeopleDAO(IPeopleDAO peopleDAO) {
  34. this.peopleDAO = peopleDAO;
  35. }
  36. }

效果图:

接着要在applicationContext.xml中添加一个bean的信息

  1. <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
  2. <property name="peopleDAO">
  3. <ref bean="PeopleDAO"></ref>
  4. </property>
  5. </bean>

整个applicationContext.xml的全部内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  7.  
  8.  
  9. <bean id="dataSource"
  10. class="org.apache.commons.dbcp.BasicDataSource">
  11. <property name="driverClassName"
  12. value="oracle.jdbc.OracleDriver">
  13. </property>
  14. <property name="url"
  15. value="jdbc:oracle:thin:@localhost:1521/orcl">
  16. </property>
  17. <property name="username" value="ssh"></property>
  18. <property name="password" value="1234"></property>
  19. </bean>
  20. <bean id="sessionFactory"
  21. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  22. <property name="dataSource">
  23. <ref bean="dataSource" />
  24. </property>
  25. <property name="hibernateProperties">
  26. <props>
  27. <prop key="hibernate.dialect">
  28. org.hibernate.dialect.Oracle9Dialect
  29. </prop>
  30. </props>
  31. </property>
  32. <property name="mappingResources">
  33. <list>
  34. <value>com/ssh/people/People.hbm.xml</value></list>
  35. </property>
  36. </bean>
  37.     <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
  38. <property name="sessionFactory">
  39. <ref bean="sessionFactory" />
  40. </property>
  41. </bean>
  42.  
  43. <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
  44. <property name="peopleDAO">
  45. <ref bean="PeopleDAO"></ref>
  46. </property>
  47. </bean>
  48.  
  49. </beans>

创建处理action的文件,新建com.ssh.action包,新建LoginAction.java文件

 

  1. package com.ssh.action;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import com.ssh.people.People;
  5. import com.ssh.service.IPeopleService;
  6.  
  7. public class LoginAction extends ActionSupport{
  8. private String username;
  9. private String password;
  10. private IPeopleService peopleService;
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getPassword() {
  18. return password;
  19. }
  20. public void setPassword(String password) {
  21. this.password = password;
  22. }
  23. public IPeopleService getPeopleService() {
  24. return peopleService;
  25. }
  26. public void setPeopleService(IPeopleService peopleService) {
  27. this.peopleService = peopleService;
  28. }
  29. public String execute(){
  30. People people = peopleService.getPeopleByUsername(username);
  31. if(people == null) return "error";
  32. if(people.getPassword().equals(password))
  33. return "success";
  34. else return "error";
  35. }
  36. public void validate(){
  37. if(username == null || username.length() == 0){
  38. super.addActionError("用户名不能为空");
  39. }
  40. if(password == null || password.length() == 0){
  41. super.addActionError("密码不能为空");
  42. }
  43. }
  44. }

配置applicationContext.xml文件:

  1. <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
  2. <property name="peopleService" ref="peopleService"></property>
  3. </bean>

配置struts.xml文件:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <package name="com" extends="struts-default" namespace="/">
  5. <action name="login" class="loginAction">
  6. <result name="success">/index.jsp</result>
  7. <result name="input">/login.jsp</result>
  8. <result name="error">/login.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

新增login.jsp文件:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib prefix="s" uri="/struts-tags"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     <title>登录页面</title>
  12.   </head>
  13.   <body>
  14.     <s:form name="form" action="login.action" method="post">
  15.      <s:textfield name="username" label="username"></s:textfield>
  16.      <s:password name="password" label="password"></s:password>
  17.      <s:submit label="submit"></s:submit>
  18.   </s:form>
  19.   <s:actionerror/>
  20.   </body>
  21. </html>

6.1完整的struts.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <package name="com" extends="struts-default" namespace="/">
  5. <action name="login" class="loginAction">
  6. <result name="success">/index.jsp</result>
  7. <result name="input">/login.jsp</result>
  8. <result name="error">/login.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

6.2完整的web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" 
  3. xmlns="http://java.sun.com/xml/ns/javaee" 
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.   <display-name></display-name>
  8.   <welcome-file-list>
  9.     <welcome-file>index.jsp</welcome-file>
  10.   </welcome-file-list>
  11.   <filter>
  12.    <filter-name>struts2</filter-name>
  13.    <filter-class>
  14.    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  15.    </filter-class>
  16.   </filter>
  17.   <filter-mapping>
  18.    <filter-name>struts2</filter-name>
  19.    <url-pattern>/*</url-pattern>
  20.   </filter-mapping>
  21. <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->  
  22.   <context-param>
  23.   <param-name>contextConfigLocation</param-name>
  24.   <param-value>
  25.  /WEB-INF/applicationContext.xml
  26.   </param-value>
  27.   </context-param>
  28.   
  29.   <!-- 对Spring容器进行实例化-->  
  30.   <listener>
  31.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  32.   </listener>
  33.   <listener>
  34.     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  35.   </listener>
  36.  </web-app>

6.3完整的applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  7.  
  8.  
  9. <bean id="dataSource"
  10. class="org.apache.commons.dbcp.BasicDataSource">
  11. <property name="driverClassName"
  12. value="oracle.jdbc.OracleDriver">
  13. </property>
  14. <property name="url"
  15. value="jdbc:oracle:thin:@localhost:1521/orcl">
  16. </property>
  17. <property name="username" value="ssh"></property>
  18. <property name="password" value="1234"></property>
  19. </bean>
  20. <bean id="sessionFactory"
  21. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  22. <property name="dataSource">
  23. <ref bean="dataSource" />
  24. </property>
  25. <property name="hibernateProperties">
  26. <props>
  27. <prop key="hibernate.dialect">
  28. org.hibernate.dialect.Oracle9Dialect
  29. </prop>
  30. </props>
  31. </property>
  32. <property name="mappingResources">
  33. <list>
  34. <value>com/ssh/people/People.hbm.xml</value></list>
  35. </property>
  36. </bean>
  37. <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
  38. <property name="sessionFactory">
  39. <ref bean="sessionFactory" />
  40. </property>
  41. </bean>
  42.  
  43. <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
  44. <property name="peopleDAO">
  45. <ref bean="PeopleDAO"></ref>
  46. </property>
  47. </bean>
  48. <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
  49. <property name="peopleService" ref="peopleService"></property>
  50. </bean>
  51.  
  52. </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

posted @ 2017-08-07 20:50  四叶草Grass  阅读(632)  评论(0编辑  收藏  举报