SSH2 Step by Step- Step 1 Struts2初步配置学习

SSH2(Struts2,Spring,Hibernate) Struts进行流程控制,Spring进行业务流转,Hibernate进行数据库操作的封装!
先把SSH2的每个框架的版本列一下,因为不同的框架版本,需要的Jar文件都有所区别:
开发环境: Eclipse 3.7 (Indigo) + Win7 + Tomcat 7 
SSH2版本:

  1. struts-2.2.3.1
  2. Spring3.1.0
  3. Hibernate4.0.0
这里的整合思路如下:
  1. 先配置Strut2的环境
  2. 将Struts2和Hibernate整合
  3. 将Struts2和Spring整合
  4. 将Spring和Hibernate整合
这里Struts升级到了Struts2,和Strut1有了很大的区别,因此配置方面也不太一样。闲话少说,马上开始。
1. 下载Struts 开发包: http://www.apache.org  (搜索Struts的Project) -- 不要告诉我你找不到
下载最新的 struts-2.2.3.1

解压缩后会发现lib目录下N多jar文件,下面这几个是必不可少的(一共8个,跟我搜索到的6个或者7个答案不一样,少一个运行Tomcat7的服务都会有问题)

2. 在Ecplise中新建一个Dynamic web Project,将上面的8个Jar文件复制到WebConent/WEB-INF/lib目录下
3. 在WEB-INF下新建一个web.xml文件,这个是启动web服务必须要用滴. 内容如下:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app   
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns="http://java.sun.com/xml/ns/javaee"   
  5.         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  6.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  7.         id="WebApp_ID" version="2.5">  
  8.       
  9.     <!-- Struts2 configuration -->  
  10.     <filter>   
  11.          <filter-name>struts2</filter-name>   
  12.          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   
  13.      </filter>   
  14.      <filter-mapping>   
  15.          <filter-name>struts2</filter-name>   
  16.          <url-pattern>/*</url-pattern>   
  17.      </filter-mapping>   
  18.        
  19. </web-app>  

注意:上面的Filter是StrutsPrepareAndExecuteFilter,这个是最新的Filter

4. 在src目录下新建一个struts.xml文件,这个是action调用必须要用滴,现在什么都不用写,放个空壳先

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <!DOCTYPE struts PUBLIC   
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.      "http://struts.apache.org/dtds/struts-2.0.dtd">   
  5. <struts>   
  6.   
  7. </struts>  
5. 写个Action类测试一下,注意这里用到了一些Struts的Feature,直接运行一下就能看到了(default action会调用execute方法,如果需要指定运行某个Method,直接在Struts.xml中指定 method="xxx"即可):
[java]  view plain copy print ?
  1. package test;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. //action类一般都继承ActionSupport  
  5. public class LoginAction extends ActionSupport {  
  6.   
  7.         public String username;  
  8.         public String password;  
  9.           
  10.         public String execute()  
  11.         {  
  12.             if (!username.equals("admin"))  
  13.             {  
  14.                 super.addFieldError("username""用户名错误!");  
  15.                 return ERROR;  
  16.             }  
  17.               
  18.             if (!password.equals("admin"))  
  19.             {  
  20.                 super.addFieldError("password""密码错误!");  
  21.                 return ERROR;  
  22.             }  
  23.               
  24.             return SUCCESS;  
  25.         }  
  26.           
  27.         public void validate()  
  28.         {  
  29.             if ((null == username) || (0==username.length()))  
  30.             {  
  31.                 super.addActionError("用户名不能为空!");  
  32.             }  
  33.               
  34.             if ((null == password) || (0 == password.length()))  
  35.             {  
  36.                 super.addActionError("密码不能为空!");  
  37.             }  
  38.         }  
  39. }  
6. 开始配置struts.xml, 在第5步的基础上增加一个package和Action:
[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <!DOCTYPE struts PUBLIC   
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.      "http://struts.apache.org/dtds/struts-2.0.dtd">   
  5. <struts>   
  6.     <package name="struts2" extends="struts-default">  
  7.         <action name="login" class="test.LoginAction">  
  8.             <result name="success" >index.jsp</result>  
  9.             <result name="input">login.jsp</result>  
  10.             <result name="error">login.jsp</result>  
  11.         </action>  
  12.     </package>  
  13. </struts>  

注意:

  1. result节点会根据LoginAction中execute方法的返回值来判断需要转到哪个jsp文件显示
  2. 如果需要指定执行LoginAction某个Method,可以增加method,例如: <action name="login" class="test.LoginAction"method="xxx">

7. 写个Login.jsp文件测试一下
login.jsp
[html]  view plain copy print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3.       
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9. <html>  
  10. <head>  
  11.     <base href="<%=basePath%>">  
  12.     <title>Struts Test</title>  
  13.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="easyTalk">  
  18.     <meta http-equiv="description" content="This is my page">  
  19. </head>  
  20. <body>  
  21.     <s:form name="loginFrm" action="login">  
  22.         <s:textfield name="username" label="username"></s:textfield>  
  23.         <s:textfield name="password" label="password"></s:textfield>  
  24.         <s:submit label="submit"></s:submit>  
  25.     </s:form>  
  26.     <s:actionerror/>  
  27. </body>  
  28. </html>  

上面用到了struts2的一些tag,具体使用方法自己百度...

成功后转到index.jsp (return string ="success")
[html]  view plain copy print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3.       
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9. <html>  
  10. <head>  
  11.     <base href="<%=basePath%>">  
  12.     <title>Struts Test Result</title>  
  13.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="easyTalk">  
  18.     <meta http-equiv="description" content="This is my page">  
  19. </head>  
  20. <body>  
  21.         <p> 用户名:<s:property value="username"/></p>  
  22.         <p> 密 码 :<s:property value="password" />  
  23. </body>  
  24. </html>  

8. 测试结果:
输入错误用户名会有自动提示:

输入正确用户名和密码:

测试通过!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值