上图来源于Struts2官方站点,是Struts 2 的整体结构。
一个请求在Struts2框架中的处理大概分为以下几个步骤
1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求
2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做
ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有
帮助,例如:SiteMesh Plugin)
3 接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需
要调用某个Action
4 如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给
ActionProxy
5 ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的
Action类
6 ActionProxy创建一个ActionInvocation的实例。
7 ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦
截器(Intercepter)的调用。
8 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回
结果。返回结果通常是(但不总是,也可 能是另外的一个Action链)一个需要被表示的
JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2 框架中继承的标签。在
这个过程中需要涉及到ActionMapper
在上述过程中所有的对象(Action,Results,Interceptors,等)都是通过
ObjectFactory来创建的。
Struts2的开发步骤主要分为:
1、准备类库。至少需要如下5个类库
struts2-core-2.0.11.jar
xwork-2.0.4.jar
ognl-2.6.11.jar
freemarker-2.3.8.jar
commons-logging-1.0.4.jar
2、在web.xml文件中配置FilterDispatcher
3、开发action。针对每个功能点,编写一个action类
4、编写相关的结果页面。针对action返回的结果代码,编写相应的结果页面
5、在Web应用程序的WEB-INF/classes目录下(使用eclipse开发只需编写在src下),对action进行配置,将action与结果页面关联在一起
根据上面的步骤写一个最简单的例子
1. 创建struts2Demo 项目
2.导入包上面的5个包,(建议从官方下载好些,个人当时在网上随便下载了一个,不知道哪里报错,所以最好官方下载)
下面是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2Demo</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
下面是struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="example" extends="struts-default">
<action name="test" class="example.Test" >
<result>/example/HelloWorld.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>
3.创建action类
package example;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport{
@Override
public String execute() throws Exception {
ServletActionContext.getContext().getSession();
ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", username);
// TODO Auto-generated method stub
System.out.println("username="+username+" ----password="+password);
return ActionSupport.SUCCESS;
}
private String username; //这里必须是index.jsp 页面的username 必须保持一致不然接收不到
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password; //这里必须是index.jsp 页面的password必须保持一致不然接收不到
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
4.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="test.action" method="post" >
用户名 <input type="text" name='username' /><br>
密码 <input type="text" name='password'><br><br>
<input type='submit' />
</form>
<br>
<hr>
</body>
</html>
//跳转页面 HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="HelloWorld.message"/></title>
</head>
<body>
${request.username} 你好!
</body>
</html>
hibernate 原理:
启动Hibernate
构建Configuration实例,初始化该实例中的所有变量 Configuration cfg = new Configuration().configure();
加载hibernate.cfg.xml文件至该实例内存
通过hibernate.xfg.xml文件中的mapping节点配置,加载hbm.xml文件至该实例内存
利用上面创建的Configuration实例构建一个SessionFactory实例 SessionFactory sf = cfg.buildSessionFactory();
由上面得到的SessionFactory实例创建连接 Session s = sf.openSession();
由上面得到的Session实例创建事务操作接口Transaction的一个实例tx Transaction tx = s.beginTransaction();
通过Session接口提供的各种方法操作数据库的访问
提交数据库的操作结果 tx.commit();
关闭Session链接 s.close();