SSH框架搭建demo

1、新建Java Web工程

SSH框架搭建demo_第1张图片

 

 

2、添加Struts2.1框架支持

 

 

去除冲突包:antlr-2.7.2.jar

 

 

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

2.1配置web.xml文件

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

 
  1.  
  2.   
  3.     struts2
  4. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  5.   
  6.   
  7.     struts2
  8.     /*
  9.   

2.2配置struts.xml文件

 
  1.  

3、添加Spring框架支持

 

 一般添加这6个支持包

 

 

效果图:

                                      

3.1配置web.xml文件 

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

 
  1.   
  2. contextConfigLocation
  3. /WEB-INF/applicationContext.xml
  4.   
  5.     
  6.   
  7.     org.springframework.web.context.ContextLoaderListener
  8.   
  9.   
  10.     org.springframework.web.context.request.RequestContextListener
  11.   

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.  
  9. public People findById(String id);
  10.  
  11. public List findByUsername(Object username);
  12.  
  13. public void save(People people);
  14.  
  15. }

把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.  
  7. public People getPeopleById(String id);
  8.  
  9. public People getPeopleByUsername(String username);
  10.  
  11. public void addPeople(People people);
  12.  
  13. }

在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.  
  13. public People getPeopleById(String id) {
  14. return peopleDAO.findById(id);
  15. }
  16.  
  17. public People getPeopleByUsername(String username) {
  18. List list = peopleDAO.findByUsername(username);
  19. if(list.size() == 0){
  20. return null;
  21. }else{
  22. return (People)list.get(0);
  23. }
  24. }
  25.  
  26. public void addPeople(People people) {
  27. peopleDAO.save(people);
  28. }
  29.  
  30. public IPeopleDAO getPeopleDAO() {
  31. return peopleDAO;
  32. }
  33.  
  34. public void setPeopleDAO(IPeopleDAO peopleDAO) {
  35. this.peopleDAO = peopleDAO;
  36. }
  37. }

效果图:

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

 

整个applicationContext.xml的全部内容

 
  1. xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6.  
  7. class="org.apache.commons.dbcp.BasicDataSource">
  8. value="oracle.jdbc.OracleDriver">
  9. value="jdbc:oracle:thin:@localhost:1521/orcl">
  10. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  11. org.hibernate.dialect.Oracle9Dialect
  12. com/ssh/people/People.hbm.xml
  13.  
  14.     
  15.  
  16.  

创建处理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.  
  9. private String username;
  10. private String password;
  11. private IPeopleService peopleService;
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. public IPeopleService getPeopleService() {
  25. return peopleService;
  26. }
  27. public void setPeopleService(IPeopleService peopleService) {
  28. this.peopleService = peopleService;
  29. }
  30.  
  31. public String execute(){
  32. People people = peopleService.getPeopleByUsername(username);
  33. if(people == null) return "error";
  34. if(people.getPassword().equals(password))
  35. return "success";
  36. else return "error";
  37. }
  38.  
  39. public void validate(){
  40. if(username == null || username.length() == 0){
  41. super.addActionError("用户名不能为空");
  42. }
  43. if(password == null || password.length() == 0){
  44. super.addActionError("密码不能为空");
  45. }
  46. }
  47. }

配置applicationContext.xml文件:

 

配置struts.xml文件:

 
  1. /index.jsp
  2. /login.jsp
  3. /login.jsp

新增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.   
  8.     ">
  9.     登录页面
  10.   
  11.   
  12.     
  13.      
  14.      
  15.      
  16.   
  17.   
  18.   

6.1完整的struts.xml文件

 
  1. /index.jsp
  2. /login.jsp
  3. /login.jsp

6.2完整的web.xml文件

 
  1. xmlns="http://java.sun.com/xml/ns/javaee" 
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  5.   
  6.   
  7.     index.jsp
  8.   
  9.   
  10.    struts2
  11.   
  12.    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  13.   
  14.   
  15.   
  16.    struts2
  17.    /*
  18.   
  19.   
  20.   
  21.   contextConfigLocation
  22.   
  23.  /WEB-INF/applicationContext.xml
  24.   
  25.   
  26.   
  27.     
  28.   
  29.     org.springframework.web.context.ContextLoaderListener
  30.   
  31.   
  32.     org.springframework.web.context.request.RequestContextListener
  33.   
  34.  

6.3完整的applicationContext.xml

 
  1. xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6.  
  7. class="org.apache.commons.dbcp.BasicDataSource">
  8. value="oracle.jdbc.OracleDriver">
  9. value="jdbc:oracle:thin:@localhost:1521/orcl">
  10. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  11. org.hibernate.dialect.Oracle9Dialect
  12. com/ssh/people/People.hbm.xml
  13.  
  14.  
  15.  
  16.  

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运行访问

http://localhost:8080/SSH_Demo/login.jsp

8、常见问题

1)  关于 myeclipse 里面没有 add struts capabilities 问题解决方法

高版本的 myeclipse 里面的 add struts capabilities 在如下路径中:(当你加载过一次之后,这个 add struts capabilities就不会出现。

解决方法:把 .project 文件的关于struts的配置删除掉即可

 

 

 附件源码

下载地址:  http://pan.baidu.com/s/1midkdCG

转载于:https://my.oschina.net/u/3616609/blog/1486894

你可能感兴趣的:(SSH框架搭建demo)