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

2023-10-30 02:38

本文主要是介绍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. 测试结果:
输入错误用户名会有自动提示:

输入正确用户名和密码:

测试通过!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

这篇关于SSH2 Step by Step- Step 1 Struts2初步配置学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/305043

相关文章

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示