码迷,mamicode.com
首页 > 其他好文 > 详细

ssh框架构建项目详解--基本概念和struts2

时间:2017-05-28 22:31:20      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:import   获取   查询   ram   names   tags   tle   evm   应该   

1.ssh是struts+spring+hibernate的继承框架,是目前比较流行的框架,但是已经不如ssm了.

2.集成ssh从职责上分为四层(类似于servlet+jsp+jdbc做项目的三层构架:视图,控制,业务....),分别是表示层,业务逻辑层,数据持久层,域模块层,这样分层可以让程序员短时间内构建一个结构清晰,复用性高,维护方便的web应用程序

  a.使用struts作为系统的整体基础架构,负责mvc的分离(模型,视图,控制器),

  b.在struts框架的模型部分,控制业务反转!

  c.利用hibernate框架对持久层提供支持,

  d.spring做管理,对hibernate和struts进行管理

3.基本思路

  a.面对对象思维,根据需求提出一些模型,将这些模型实现为基本的java对象

  b.编写基本的dao接口,采用hiberna架构来实现dao类(sessionfactory对jdbc进行封装..简化了数据访问层的编写)

  c.spring作为总管家,对hibern和struts进行统筹管理

4.业务流程

  a.在表示层中,通过jsp页面实现交互界面,负责接收请求和传送相应(request&response)

  b.在struts配置文件中(struts-config.xml)将ActionServlet接收到的数据委派给相应的Action进行处理

  c.在业务逻辑层中,管理服务组件的spring Ioc容器负责向Action提供业务模型(service),和该业务的数据处理组件(dao)共同完成业务逻辑,也就是自动注入...并且提供事务处理,缓冲池等组件提升系统性能和数据完整性

  d.在持久层中(也就是执行增改查)则依赖于hibernate对象映射和数据库交互.处理dao请求的数据并返回结果

 

5.总结:采用ssh框架进行开发,实现了对mvc的分离,也实现了业务逻辑和持久层的分离,最终目的,就是不论前端如何变化,模型要改动的都很少.....总体而言提高了开发效率,提高了可维护性和复用性...

 

6.框架结构

  1.struts,一般我们开发都使用maven构架,因此在pom.xml添加的依赖jar包..struts2的依赖jar如下;

    技术分享

          commons和commons-io是 apache commons的jar包

    ognl提供了OGNL表达式

    struts2-core提供了struts2核心包

    xwork-core是由于struts2很多事是基于webwork的,因此也需要这个jar包

    依赖坐标可以去

      http://mvnrepository.com/  

      查询和复制

    

    我们提供三个jsp页面

    这是login.jsp登录页面

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user"/>
<s:textfield name="password" key="pass"/>
<s:submit key="login"/>
</s:form>
</body>
</html>

 

  这是登录成功的welcome.jsp页面

 

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
    <s:text name="succTip">
        <s:param>${sessionScope.user}</s:param>
    </s:text><br/>
</body>
</html>

这是登录失败的error.jsp页面

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title><s:text name="errorPage"/></title>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
    <s:text name="failTip"/>
</body>
</html>


struts-config.xml配置如下(配置文件放在根目录下)
<struts>
     6 <!-- 指定全局国际化资源文件 --> 7 <constant name="struts.custom.i18n.resources" value="mess"/> 8 <!-- 指定国际化编码所使用的字符集 --> 9 <constant name="struts.i18n.encoding" value="GBK"/> 10 <!-- 所有的Action定义都应该放在package下 --> 11 <package name="test" extends="struts-default"> 12 <action name="login" class="com.test.action.LoginAction"> 13 <result name="error">/error.jsp</result> 14 <result name="success">/welcome.jsp</result> 15 </action> 16 </package> 17 </struts>

     所有的 action定义都应该放在package下,class后面是全路径名称,result是action返回的结果名称,和指定的页面

    其constant配置常用配置如下

  

<constant name="struts.custom.i18n.resources" value="mes"></constant>
<constant name="struts.multipart.parser" value="jakarta"></constant>
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action,," />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" />

  

 

  下面是loginAction的代码

  

package com.test.action;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 
 6 public class LoginAction extends ActionSupport {
 7     private String username;//从页面获取的参数名称
 8     private String password;
 9   //通国get/set方法获取页面请求数据,与页面名称要一致,如果是对象要写成 user.userName
10     public String getUsername() {
11         return username;
12     }
13 
14     public void setUsername(String username) {
15         this.username = username;
16     }
17 
18     public String getPassword() {
19         return password;
20     }
21 
22     public void setPassword(String password) {
23         this.password = password;
24     }
25   //核心action方法,返回的是一个字符串,该字符创要和struts-config.xml配置中的action配置的result配置中的名称一致...
26     public String execute() throws Exception {
27         if (getUsername().equals("xingoo") && getPassword().equals("123")) {
28             ActionContext.getContext().getSession().put("user", getUsername());
29             return SUCCESS;
30         }else{
31             return ERROR;
32         }
33     }
34 }

    

    

 

ssh框架构建项目详解--基本概念和struts2

标签:import   获取   查询   ram   names   tags   tle   evm   应该   

原文地址:http://www.cnblogs.com/ou134568/p/6916761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!