面对不同种类的处理结果——result type=

2024-05-16 15:18

本文主要是介绍面对不同种类的处理结果——result type=,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Struts2中有很多不同种类的处理结果,有action也有视图文件。比较常见的情景就是我们要一个action链接到另一个action应该怎么做呢?我在这里就做了一个小例子

JSP文件内容



<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><!-- chain链式结果类型,从一个action跳到另一个action --><s:form action="start"><s:submit value="chain链式"/></s:form><!-- 源代码类型 --><s:form action="plainText"><s:submit value="plainText源代码"/></s:form><!-- 请求转发 --><s:form action="dispatcher"><s:submit value="dispatcher转发到JSP视图文件"/></s:form><!-- 请求重定向 --><!-- 注意:重定向和请求转发可是有区别的哦,比较明显的是URL变化 --><s:form action="redirect"><s:submit value="redirect重定向到JSP视图文件"/></s:form><!-- 请求重定向到一个action --><s:form action="redirectAction"><s:submit value="redirectAction重定向到一个Action中"/></s:form><!-- 请求重定向,可以自定义的???这里只能是英文的,我就算在xml中设置的charSet也没有用,有知道的前辈求解一下这个问题,十分感谢--><s:form action="redirect-custom"><s:textfield name="myUrl"/><s:submit/></s:form><!-- 测试全局结果,如果一个action找不到匹配 --><s:form action="login!global"><s:submit value="TestGlobal"/></s:form></body>
</html>

struts.xml文件内容

<?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.enable.DynamicMethodInvocation" value="true" /><constant name="struts.devMode" value="true" /><constant name="struts.custom.i18n.resources" value="test"/><package name="default" namespace="/" extends="struts-default"><global-results><result name="TestGlobal">/content/TestGlobal.jsp</result></global-results><action name="login" class="action.LoginAction"><result name="success">/content/success.jsp</result></action><action name="start" class="action.ActionOne"><result type="chain">end</result></action><action name="end" class="action.ActionTwo"><result>/content/success.jsp</result></action><action name="plainText"><result type="plainText"><param name="location">/content/success.jsp</param><param name="charSet">UTF-8</param></result></action><action name="dispatcher"><result type="dispatcher">/content/success.jsp</result></action><action name="redirect"><result type="redirect">/content/success.jsp</result></action><action name="redirectAction"><result type="redirectAction"><param name="actionName">TestRedirectAction</param><param name="namespace">/</param></result></action><action name="redirect-custom" class="action.RedirectCustom"><result type="redirect"><param name="location">testRedirect.action?myUrl=${myUrl}</param></result></action><action name="testRedirect" class="action.TestRedirect"><result>/content/success.jsp</result></action><action name="TestRedirectAction"><result>/content/success.jsp</result></action></package></struts>
action中的类的内容

ActionOne.java

package action;import com.opensymphony.xwork2.ActionSupport;public class ActionOne extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("This is ActionOne");return super.execute();}
}



ActionTwo.java

package action;import com.opensymphony.xwork2.ActionSupport;public class ActionTwo extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("This is ActionTwo");return super.execute();}
}



LoginAction.java

package action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("This is execute");return super.execute();}public String method1() throws Exception {System.out.println("This is one");return SUCCESS;}public String method2() throws Exception {System.out.println("This is two");return SUCCESS;}public String global() throws Exception {System.out.println("This is glboal()");return "TestGlobal";}
}



RedirectCustom.java

package action;import com.opensymphony.xwork2.ActionSupport;public class RedirectCustom extends ActionSupport {private String myUrl;public String getMyUrl() {return myUrl;}public void setMyUrl(String myUrl) {this.myUrl = myUrl;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("这里是RedirectCustom的execute(),接收到的myUrl为" + getMyUrl());return super.execute();}
}



TestRedirect.java

package action;import com.opensymphony.xwork2.ActionSupport;public class TestRedirect extends ActionSupport {private String myUrl;public String getMyUrl() {return myUrl;}public void setMyUrl(String myUrl) {this.myUrl = myUrl;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("这里是TestRedirect的execute(),接收到的myUrl值为:" + getMyUrl());return super.execute();}
}
呼呼~总之你做好这几个文件就好了,之后慢慢看action是如何跳转的。我先撤了

这篇关于面对不同种类的处理结果——result type=的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/995276

相关文章

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi