struts2介绍

时间:2023-03-09 06:11:22
struts2介绍

struts2简介

Struts2框架发展

Struts于2000年5月由Craig McClanahan发起,并于 2001年7月发布了1.0版本,Struts一出现便大受欢迎,更成为了以后几年内web 开发的实际标准,Struts2是Struts的下一代产品,是在WebWork的技术基础上进行了合并

Struts2框架特点

1、基于Action的实现MVC框架 

2、拥有积极活跃的成熟社区 

3、使用Annotation和XML配置选项 

4、使用插件来扩展或修改框架特性 

5、与Spring,SiteMesh 和Tiles的集成 

6、使用OGNL表达式语言 

7、使用多种视图选项 

Struts2基本的工作过程

1、过滤器FilterDispatcher拦截所有请求

2、如果请求的是Action组件, 相应的拦截器会逐个对请求进行拦截处理

3、拦截器处理完毕, 会调用Action组件的默认业务方法execute()进行业务请求处理

4、根据业务方法返回结果值来选择适当的视图

Struts2 开发所需要的必要包(5个)

commons-fileupload-1.2.1.jar

freemarker-2.3.13.jar

ognl-2.6.11.jar

struts2-core-2.1.6.jar

xwork-2.1.2.jar

在web.xml中配置Struts2的过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <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>

</web-app>

HelloWorld的简单实例

(1)设计action组件

package demo.web;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction{

    private String content;//保存回应结果数据

    public String getContent(){
return content;
}
public void setContent(String content){
this.content = content;
} //响应用户请求,业务处理方法
public String execute (){
content = "Hello World";
return Action.SUCCESS;
}
}

(2)设计视图组件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %> <!-- 引入struts2标签 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>Struts2 Demo</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>
<p style="color: red;"><s:property value="content"/></p> <!-- action中定义的content属性名 两种方式 -->
<p style="color: blue;">${content}</p>
</body>
</html>

(3)配置Action和视图结果

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"> <struts>
<package name="helloworld" extends="struts-default"> <!-- name:自定义包名 extends:继承自struts2的缺省包struts-default -->
<action name="hello" class="demo.web.HelloWorldAction"> <!-- name:action请求的映射名 class:action组件的全名 -->
<result name="success">/helloworld.jsp</result><!-- name:action中execute对应的结果别名 /helloworld.jsp:视图页面中的URL地址 -->
</action>
</package> </struts>

(4)调试运行

在地址栏输入URL来请求Action组件,其URL地址格式如下:

http://localhost:8080/Struts2_1/hello.action或者http://localhost:8080/Struts2_1/hello!execute

struts2介绍

源码请到struts2简介下载(jar包需要自己加上)