1. Struts2处理表单数据

例1.1 创建Java Web项目,编写一个Action对象,处理对表单提交的数据,模拟实现对指定用户的问候。

(1)创建Java Web项目,将Struts的支持类型库添加到WEB-INF目录下的lib文件夹中。之后在web.xml文件中注册Struts2提供的过滤器。过滤器代码如下:

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>java_struts2</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>

(2)创建一个Action对象,其名称为GreetingAction。实例中通过继承于ActionSupport类进行创建,其关键代码如下:

package com.cn.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;
}
}

(3)在Web项目的源码文件夹中,创建名称为struts.xml的配置文件,在该文件中配置GreetingAction。关键代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default" namespace="/">
<!-- 定义action -->
<action name="greeting" class="com.cn.action.GreetingAction">
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
<!-- 定义失败的映射页面 -->
<result name="error">error.jsp</result>
</action>
</package>
</struts>

(4)创建程序中的首页面index.jsp,在该页面中编写一个表单,它的提交地址为greeting.action。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="greeting.action" method="post">
请输入你的姓名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>

(5)创建success.jsp页面。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<font color="red">
<s:property value="username"/>
</font>
,您好!
<br>
欢迎来到本站。
</body>
</html>

success.jsp页面中的<s:property>标签是Struts2提供的标签库,主要用于输出Action对象中的信息。

(6)创建名称为error.jsp页面。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<font color="red">错误:您没有输入用户名!</font>
</body>
</html>

运行上面的程序,如果出现No result defined for action com.cn.action.GreetingAction and result success错误,在struts.xml的配置文件的package里面增加 namespace="/" 告诉系统是从根本录寻找即可。

2. 使用Map类型的request、session和application

例2.1 通过ActionContext对象获取Map类型的requet、session、和application,分别为这3个对象设置一个info属性并赋值,然后在JSP页面获取3种作用域下的info属性信息。

1)创建Java Web项目,将Struts的支持类型库添加到WEB-INF目录下的lib文件夹中。之后在web.xml文件中注册Struts2提供的过滤器。过滤器代码如下:

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>java_struts2</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>

(2)创建名称为TestAction的类,该类继承于ActionSupport类,是一个Action对象。在这个类中分别创建Map类型的request、session和application,并在execut()方法中对其进行操作。关键代码如下:

package com.cn.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
//Map类型的request
private Map<String, Object> request;
//Map类型的session
private Map<String, Object> session;
//Map类型的application
private Map<String, Object> application;
//构造方法
@SuppressWarnings("unchecked")
public TestAction() {
// 获取ActionContext对象
ActionContext context = ActionContext.getContext();
//获取Map类型的request
request = (Map<String, Object>) context.get("request");
//获取Map类型的session
session = context.getSession();
//获取Map类型的application
application = context.getApplication();
}
/**
* 请求处理方法
* @return String
*/
public String execute() throws Exception {
// 字符串信息
String info="明日科技";
//向request添加信息
request.put("info", info);
//向session添加信息
session.put("info", info);
//向application添加信息
application.put("info", info);
//成功返回
return SUCCESS;
}
}

(3)在Web项目的源码文件夹中,创建名称为struts.xml的配置文件,在该配置文件中配置TestAction。关键代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default" namespace="/">
<!-- 定义action -->
<action name="testAction" class="com.cn.action.TestAction">
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
</action>
</package>
</struts>

(4)创建TestAction处理成功的返回页面success.jsp,在该页面中分别获取JSP内置对象request、session、application的info属性的值,并将这些值输出到JSP页面中。具体代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
request范围内的info值:
<font color="red"><%=request.getAttribute("info") %></font>
<br>
session范围内的info值:
<font color="red"><%=session.getAttribute("info") %></font>
<br>
application范围内的info值:
<font color="red"><%=application.getAttribute("info") %></font>
</body>
</html>

(5)创建程序的首页index.jsp,在该页面中编辑一个超链接,将这个超链接指向testAction。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="testAction.action">Map类型的request、session、application</a>
<br>
</body>
</html>

3. 使用拦截器

在Struts2框架中,如果创建了一个拦截器对象,需要对拦截器进行配置才可以应用到Action对象上。其中拦截器的创建比较简单,可以通过继承AbstractInterceptor对象进行创建,而它使用<interceptor-ref>标签进行配置。

例3.1 为Action对象配置输出执行时间的拦截器,查看执行Action所需要的时间。

(1)创建Java Web项目,将Struts的支持类型库添加到WEB-INF目录下的lib文件夹中。之后在web.xml文件中注册Struts2提供的过滤器。

(2)创建名称为TestAction的类,该类继承于ActionSupport对象。关键代码如下:

package com.cn.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute() throws Exception{
//线程休眠1秒
Thread.sleep(1000); //方便查看执行时间
return SUCCESS;
}
}

(3)在struts.xml配置文件中配置TestAction对象,并将输出Action执行时间的拦截器timer应用到TestAction中。关键代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 声明常量(开发模式) -->
<constant name="struts.devMode" value="true"></constant>
<!-- 声明常量(在Struts的配置文件修改后,自动加载) -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default" namespace="/">
<!-- 定义action -->
<action name="testAction" class="com.cn.action.TestAction">
<!-- 配置拦截器 -->
<interceptor-ref name="timer"/>
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
</action>
</package>
</struts>

在TestAction对象的配置中,为其配置了一个拦截器对象timer,其作用是输出TestAction执行的时间。

(4)创建程序的首页页面index.jsp和TestAction返回的页面success.jsp,具体代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="testAction.action">执行输出时间拦截器</a>
<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="index.jsp">返回</a>
<br>
</body>
</html>

在代码写完后,执行该程序,在控制台会输出TestAction执行所占用的时间。

Struts2典型应用的更多相关文章

  1. Java Web目录

    1. Spring持久化 2. Spring核心之IoC——依赖注入 3. Hibernate查询语言 4. Hibernate 实体关联关系映射(转载) 5. 用MyEclipse自动生成hiber ...

  2. Spring+Struts2+Mybatis框架搭建时的常见典型问题

    搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...

  3. Struts2 基础典型应用

    例子 下面就是运用Struts2 实现的例子的运行效果 输入正确名字 不输入直接点击提交按钮 在首页面中输入名称,点击提交按钮,显示欢迎界面. 如果没有名称,点击提交按钮,就显示错误界面. ===== ...

  4. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  5. SSH面试题(struts2+Spring+hibernate)

    struts2 + Spring +hibernate Hibernate工作原理及为什么要用?   原理:   1.读取并解析配置文件   2.读取并解析映射信息,创建SessionFactory ...

  6. 【学习笔记】Struts2之配置处理结果

        Action只是Struts2控制器的一部分,所以它不能直接生成对浏览者的响应.Action只负责生成响应的视图组件,通常是JSP页面,而Action会为JSP页面提供显示数据.     Ac ...

  7. 一口气从CSS讲到Servlet再到JSP、Struts2,清蒸JavaWeb的前前后后。

    B/S系统就是Browser/Server,浏览器/服务器系统,即,客户在浏览器操作,而代码实现的具体处理以及数据库操作等,则由后台服务器来完成,男耕女织,相得甚欢.比如我们查询成绩,我们通过浏览器输 ...

  8. 【Spring】Spring框架之Struts2和Spring的优点

    Java Web开发使用Structs2和Spring框架的好处 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术 ...

  9. struts2总结一:MVC设计模式

    设计模式 一.什么是编程里面的设计模式? 1.设计模式是一套被反复使用,多数人知晓的,代码设计经验的总结. 2.模式必须是典型问题(不是个别问题)的解决方案. 二.设计模式的作用 1.解决一类问题的成 ...

随机推荐

  1. sed实践

    后来也找到一篇文章讲的很详细: http://www.cnblogs.com/ctaixw/p/5860221.html --------------------------------------- ...

  2. 【LOJ】#2586. 「APIO2018」选圆圈

    题解 不旋转坐标系,TLE,旋转坐标系,最慢一个点0.5s--maya,出题人数据水平很高了-- 好吧,如果你不旋转坐标系,写一个正确性和复杂度未知的K - D树,没有优化,你可以得到87分的好成绩 ...

  3. 【LOJ】#2544. 「JXOI2018」游戏

    题解 九条可怜还有那么善良的一面??? 显然有些数在这个区间里没有数是它的约数,它们其中的最后一个取的一定就是\(t(p)\)的值 这样我们只需要枚举\(t(p)\)的值,这个值就是"没有任 ...

  4. php大图

    原文地址:https://laravel-china.org/articles/9450/php-fpm-vs-swoole

  5. VIM块操作

    一.可视模式 按v启用可视模式,之后移动光标可以选择. 如:     如果想整行操作,则用大写的V,再移动光标可以按行为单位进行选择. 二.列块操作 在 word中有一个功能,按alt加鼠标拖动,可以 ...

  6. 不愿看到Java开发者再做的10件事

    William F. Buckley.Jr 曾经说过,“保守主义者是那些逆着历史潮流不断喊停的人,其他人都不愿意这么做或者对他们这么做显得没有耐性”.虽然我对此了解不多,但是每次看到有Java开发人员 ...

  7. Python return语句 函数返回值

    return语句是从python 函数返回一个值,在讲到定义函数的时候有讲过,每个函数都要有一个返回值.Python中的return语句有什么作用,今天就来仔细的讲解一下. python 函数返回值 ...

  8. android aar jar

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha aar 是  安卓 类库项目的 二进制发行包.  文件扩展名 是 aar 专家 mave ...

  9. 2018 计算之道初赛第二场 阿里巴巴的手机代理商(困难)(反向可持久化Trie)

    阿里巴巴的手机代理商(困难) 阿里巴巴的手机代理商正在研究 infra 输入法的新功能.他们需要分析单词频率以改进用户输入法的体验.于是需要你在系统内核里面写一个 API. API 有如下功能: 添加 ...

  10. 【8.20校内测试】【DP】【二分+贪心】

    一开始想的贪心,可是发现贪心的问题太多了啊!只能保证当前最优,全局完全无法考虑. 所以正解是dp.预处理出前缀和,枚举每个区间,在每个点记录$now[i]$表示以$i$这个塔结尾的塔组目前的高度.$d ...