Struts2 基础典型应用
例子
下面就是运用Struts2 实现的例子的运行效果

输入正确名字

不输入直接点击提交按钮

在首页面中输入名称,点击提交按钮,显示欢迎界面。
如果没有名称,点击提交按钮,就显示错误界面。
=============================================================================================
原理:

1.当用户输入或不输入名字,并点击提交按钮后,相当于浏览器提交了一个请求(http://localhost:8080/Struts2Test/greeting.action)给web容器
2.Web容器调用Struts2过滤器的doFilter()方法(在web.xml中注册了一个Struts2提供的过滤器,所以才调用Struts2过滤器)
3.在第一步中可以看到,请求中调用的action的名称是greeting,那么Struts2就会在struts.xml中与greeting相匹配的action(如<action name="greeting" class="com.hch.action.GreetingAction">)
4.找到后将请求中的表单数据注入到这个action对象中,注入的方法是通过该对象的setXXX()方法,注入后再调用该对象的execute()方法来执行,并返回结果到struts2中,假设这里返回的结果是success。
5. Struts2 根据action对象返回的success结果,在struts.xml中找到<result name="success">success.jsp</result>信息,并将success.jsp返回给浏览器来展示,这样一次交互就结束了。
==============================================================================================
实现过程
1.创建一个Java Web项目。

2. 下载依赖类库,并解压,将其中的库文件添加到WEB-INF目录的lib文件夹中。
最新的版本的下载链接
http://struts.apache.org/download.cgi#struts251 或者访问 http://pan.baidu.com/s/1nvvoLEp

本例子使用的全部依赖库下载地址:http://pan.baidu.com/s/1gfa2q5d
3.在WEB-INF目录下创建一个web.xml文件,并在其中注册一个Struts2提供的过滤器,文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>8.2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list><!-- Struts2过滤器 -->
<filter>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- Struts2过滤器映射 -->
<filter-mapping>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器映射 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4. 创建一个Action对象,其名称是GreetingAction。
位置如下

代码如下:
package com.hch.action;
import com.opensymphony.xwork2.ActionSupport; public class GreetingAction extends ActionSupport {
private static final long serialVersionUID = 1L;
// 用户名
private String username;
// 处理请求
@Override
public String execute() throws Exception{
// 判断用户名是否有效
if(username == null || "".equals(username)){
// 返回到错误页面
return ERROR;
}else{
// 返回到成功页面
return SUCCESS;
}
}
// username属性的getter方法
public String getUsername() {
return username;
}
// username属性的setter方法
public void setUsername(String username) {
this.username = username;
}
}
5. 创建struts.xml文件
位置如下

内容如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="greeting" class="com.hch.action.GreetingAction">
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
<!-- 定义失败的映射页面 -->
<result name="error">error.jsp</result>
</action>
</package>
</struts>
6. 创建 首页面index.jsp \ 处理成功页面success.jsp \ 处理失败页面error.jsp
位置如下:

内容如下:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title>
<style type="text/css">
*{font-size: 12px;}
</style>
</head>
<body>
<form action="greeting.action" method="post">
请输入你的姓名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
<style type="text/css">
*{font-size: 12px;}
</style>
</head>
<body>
<font color="red">
<s:property value="username" />
</font>
,您好!
<br>
欢迎使用来到本站。
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误</title>
</head>
<body>
<font color="red"> 错误:您没有输入姓名!</font>
</body>
</html>
Struts2 基础典型应用的更多相关文章
- Struts2入门1 Struts2基础知识
Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...
- Struts2基础学习2
Struts2基础学习2 项目结构,测试页面与实体类 <%@ page language="java" contentType="text/html; charse ...
- Struts2基础入门
Struts2基础入门 创建一个web工程 0)导包并且创建一个核心配置文件 <?xml version="1.0" encoding="UTF-8"?& ...
- Struts2框架学习第三章——Struts2基础
本章要点 — Struts 1框架的基本知识 — 使用Struts 1框架开发Web应用 — WebWork框架的基本知识 — 使用WebWork框架开发Web应用 — 在Eclipse中整合To ...
- Struts2基础学习总结
引用自:http://www.cnblogs.com/jbelial/archive/2012/05/10/2486886.html Struts 2是在WebWork2基础发展而来的. 注意:str ...
- struts2基础——标签
一.通用标签 1.s:property (读取值栈中对象的属性值) 属性:value:指定OGNL表达式:default:OGNL表达式返回为 null 时,使用默认值:escape:是否对 HTML ...
- 经典MVC框架技术-struts2基础知识
Struts2框架简介 struts2框架是在struts1和webwork技术的基础上,进行合并的全新框架,struts2以Webwork为核心,采用拦截器来处理用户的请求,这样的设计使得业务逻辑控 ...
- (一)Struts2 基础
一.Struts简介 1.1 历史 虽然Struts 2号称是一个全新的框架,但这仅仅是相对Struts 1而言.Struts 2与Struts 1相比,确实有很多革命性的改进,但它并不是新发布的新框 ...
- struts2 基础
框架(frameWork):某一种应用的半成品 struts2: 表现层 处理与页面进行交互的相关功能 hibernate: 持久层 负责业务逻辑数据的持久化 spring: 业务层 负责复杂的业 ...
随机推荐
- 动态控制body最小高度
//动态控制body最小高度 var windowHeight = $(document).height() - 164; $(".body-content").css({ &qu ...
- Spring的常见问题及答案
目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...
- python staticmethod和classmethod(转载)
staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...
- java创建文件夹以及文件
java在创建文件的过程中如果改文件的路径不存在: 会出现下面这种情况 java.io.IOException: 系统找不到指定的路径. at java.io.WinNTFileSystem.crea ...
- 跟我一起学wpf(1)-布局
wpf常用的布局控件 Canvas,DockPanel,Grid,StackPanel,WrapPane 1 Canvas是常用的画布容器,里面可以包含多个比如之前我写的3D效果的动画,都是用Canv ...
- 用mingw-w64 编译 x64 位的ffmpeg
http://blog.sina.com.cn/s/blog_6125d067010168dt.html 工作中用到了ffmpeg x64. 发现编译出来x64的ffmpeg,很不容易.特记录下来.原 ...
- 单元测试JUnit4 Ctrl + Shift + T
单元测试 public class Calculator { public int result = 0; public int add(int operandl, int operand2) { r ...
- 【C】论‘\r’和'\n'的纯粹性
- 编码 —— PCM 编码
PCM:Pulse Code Modulation,脉冲编码调制: 1. 码率的计算 PCM约定俗成了无损编码,因为PCM代表了数字音频中最佳的保真水准,并不意味着PCM就能够确保信号绝对保真,PCM ...
- 用python做自动化测试--Python实现远程性能监控
http://blog.csdn.net/powerccna/article/details/8044222 在性能测试中,监控被测试服务器的性能指标是个重要的工作,包括CPU/Memory/IO/N ...