关于result标签type属性的三种值

本文详细介绍了Struts2框架中的三种跳转方式:转发(dispatcher)、重定向(redirect)及重定向到Action(redirectAction),并解释了它们的工作原理与使用场景。转发适用于同一请求内页面展示,保持URL不变;重定向则适用于需要改变URL的情况,并可跨应用跳转;重定向到Action则用于指定跳转到特定Action。

第一种是转发--dispatcher

这也是默认值,转发到一个页面

	<package name="customer" extends="struts-default" namespace="/customer">
		<action name="queryAll" class="com.dimples.action.CustomerAction" method="queryAll">
			<result name="success">/jsp/show.jsp</result>
		</action>
	</package>

第二种是重定向--redirect

这种属性可以重定向到一个页面,也可以重定向到一个action

<package name="xixi" extends="struts-default" namespace="/ceshi">
	<action name="test">
		<result name="success" type="redirect">test2</result>
	</action>
	<action name="test2">
		<result name="success">/jsp/show.jsp</result>
	</action>
</package>
解析:当在result标签里写的是一个action的时候,它会自动追加上.action,所以如果当我们配置的url后缀不包括action的时候,我们需要手动加上后缀,比如 test2.do

第三种是专门重定向到一个action--redirectAction

<package name="xixi" extends="struts-default" namespace="/ceshi">
	<action name="test">
		<result name="success" type="redirectAction">test2</result>
	</action>
	<action name="test2">
		<result name="success">/jsp/show.jsp</result>
	</action>
</package>

解析:这种方法会主动在test2后面智能的加上后缀,所以不用担心你url里是怎么配置的。并且跳转到一个新action的时候,它会给你主动加上这个namespace,在这里也就是 /ceshi  。所以浏览器地址变化是由/ceshi/test  转变为/ceshi/test2,所以注意重定向的这个action要和本action在同一个package里。最后就是想说说由一个action跳到一个页面时,地址拦变化是:

/ceshi/test2  --》  /jsp/show.jsp,也就是记住在写namespace时注意有 /,而action的name都是没有/的,然后写页面的时候要带上/


ps:转发和重定向区别

转发:一次请求    url不变    域中参数可传递    服务器行为    只局限于应用内部

重定向:二次请求    url改变    域中参数丢失    浏览器行为    可重定向到任意地址


using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using FengJing.Services.PlantLibModule; using FengJing.Views.OtherView; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Application = Autodesk.AutoCAD.ApplicationServices.Application; using Exception = Autodesk.AutoCAD.Runtime.Exception; [assembly: CommandClass(typeof(FengJing.CADModule.PlantManager))] namespace FengJing.CADModule { public class PlantManager { private static string _selectedPlantName = null; private const string RegAppName = "MyPlantApp"; // 属性定义与标签的映射 private static readonly Dictionary<string, string> AttributeTags = new Dictionary<string, string> { {"名称", "PLANT_NAME"}, {"类型", "PLANT_TYPE"}, {"高度(m)", "PLANT_HEIGHT"}, {"冠幅(cm)", "PLANT_CROWN"}, {"胸径(cm)", "PLANT_DIAMETER"}, {"单位", "PLANT_UNIT"}, {"观赏价", "PLANT_ORNAMENTAL"}, {"色叶期", "PLANT_COLOR_PERIOD"}, {"颜色", "PLANT_COLOR"}, {"耐湿耐旱性", "PLANT_TOLERANCE"}, {"海绵类", "PLANT_SPONGE"}, {"生长习性", "PLANT_HABIT"}, {"种植密度(株/㎡)", "PLANT_DENSITY"}, {"信息价(元)", "PLANT_PRICE"} }; [CommandMethod("INSERT_PLANT", CommandFlags.Modal)] public void InsertPlantBlock() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; RegisterRegAppName(db); try { plantService plantService = new plantService(); List<Plant> allPlants = plantService.GetAllPlants(); if (allPlants == null || allPlants.Count == 0) { ed.WriteMessage("\n数据库中没有苗木数据!"); return; } _selectedPlantName = null; Application.ShowModelessWindow(new PlantList(allPlants, this)); while (_selectedPlantName == null) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(100); if (!Application.MainWindow.Visible) return; } string plantName = _selectedPlantName; PromptPointResult ppr = ed.GetPoint("\n指定苗木插入点: "); if (ppr.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; string blockName = GenerateBlockName(plantName); if (!blockTable.Has(blockName)) { ed.WriteMessage($"\n错误:未找到名为 {blockName} 的块定义,请在CAD中预先创建!"); return; } BlockReference blockRef = InsertPlantReference(tr, db, blockName, ppr.Value); Plant plantForBinding = plantService.GetPlantByName(plantName); if (plantForBinding != null) { BlockReference blockRefForWrite = (BlockReference)tr.GetObject(blockRef.ObjectId, OpenMode.ForWrite); BindPlantAttributes(tr, blockRefForWrite, plantForBinding); ed.WriteMessage($"\n苗木 '{plantName}' 属性已绑定到块!"); } tr.Commit(); ed.WriteMessage($"\n苗木 '{plantName}' 插入成功!"); } } catch (Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } [CommandMethod("BIND_PLANT", CommandFlags.Modal)] public void BindPlantToBlock() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; RegisterRegAppName(db); try { PromptEntityOptions opts = new PromptEntityOptions("\n选择要绑定苗木属性的块: "); opts.SetRejectMessage("\n请选择一个块参照."); opts.AddAllowedClass(typeof(BlockReference), false); PromptEntityResult per = ed.GetEntity(opts); if (per.Status != PromptStatus.OK) return; plantService plantService = new plantService(); List<Plant> allPlants = plantService.GetAllPlants(); if (allPlants == null || allPlants.Count == 0) { ed.WriteMessage("\n数据库中没有苗木数据!"); return; } _selectedPlantName = null; Application.ShowModelessWindow(new PlantList(allPlants, this)); while (_selectedPlantName == null) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(100); if (!Application.MainWindow.Visible) return; } string plantName = _selectedPlantName; Plant selectedPlant = plantService.GetPlantByName(plantName); if (selectedPlant == null) { ed.WriteMessage($"\n未找到苗木: {plantName}"); return; } using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockReference blockRef = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference; BindPlantAttributes(tr, blockRef, selectedPlant); tr.Commit(); ed.WriteMessage($"\n成功绑定苗木属性到块: {plantName}"); } } catch (Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } // 绑定属性到块属性 private void BindPlantAttributes(Transaction tr, BlockReference blockRef, Plant plant) { // 1. 绑定到块属性 if (blockRef.AttributeCollection.Count > 0) { foreach (ObjectId attId in blockRef.AttributeCollection) { AttributeReference attRef = tr.GetObject(attId, OpenMode.ForWrite) as AttributeReference; if (attRef != null && AttributeTags.ContainsValue(attRef.Tag)) { var mapping = AttributeTags.FirstOrDefault(x => x.Value == attRef.Tag); if (!string.IsNullOrEmpty(mapping.Key)) { attRef.TextString = GetPlantPropertyValue(plant, mapping.Key); } } } } // 2. 同时保留XData以备其他用途 ResultBuffer rb = new ResultBuffer(); rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, RegAppName)); foreach (var prop in AttributeTags) { rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, prop.Key)); rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, GetPlantPropertyValue(plant, prop.Key))); } blockRef.XData = rb; } // 获取苗木属性 private string GetPlantPropertyValue(Plant plant, string propertyName) { switch (propertyName) { case "名称": return plant.Name; case "类型": return plant.Type?.Name ?? "未知"; case "高度(m)": return plant.Height.ToString("0.0"); case "冠幅(cm)": return plant.CrownWidth.ToString(); case "胸径(cm)": return plant.Diameter.ToString(); case "单位": return plant.Unit?.UnitName ?? ""; case "观赏价": return plant.OrnamentalValue?.OrnamentalValueName ?? ""; case "色叶期": return plant.ColorPeriod?.ColorPeriodName ?? ""; case "颜色": return plant.Color?.ColorName ?? ""; case "耐湿耐旱性": return plant.WetDroughtTolerance; case "海绵类": return plant.IsSpongePlant.ToString(); case "生长习性": return plant.Habit; case "种植密度(株/㎡)": return plant.PlantingDensity.ToString(); case "信息价(元)": return plant.Price.ToString(); default: return string.Empty; } } public void SetSelectedPlant(string plantName) { _selectedPlantName = plantName; } private string GenerateBlockName(string plantName) { return plantName .Replace(" ", "_") .Replace("/", "_") .Replace("\\", "_") .Replace("*", "") .ToUpper(); } private BlockReference InsertPlantReference( Transaction tr, Database db, string blockName, Point3d insertionPoint) { BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blockId = blockTable[blockName]; BlockTableRecord modelSpace = tr.GetObject( SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord; BlockReference plantRef = new BlockReference(insertionPoint, blockId); modelSpace.AppendEntity(plantRef); tr.AddNewlyCreatedDBObject(plantRef, true); // 创建属性引用 CreateAttributeReferences(tr, plantRef); return plantRef; } // 创建属性引用 private void CreateAttributeReferences(Transaction tr, BlockReference blockRef) { BlockTableRecord btr = tr.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId id in btr) { if (id.ObjectClass == RXClass.GetClass(typeof(AttributeDefinition))) { AttributeDefinition ad = tr.GetObject(id, OpenMode.ForRead) as AttributeDefinition; if (ad != null && !ad.Constant) { AttributeReference ar = new AttributeReference(); ar.SetAttributeFromBlock(ad, blockRef.BlockTransform); ar.Position = ad.Position.TransformBy(blockRef.BlockTransform); ar.TextString = ad.TextString; ar.Layer = blockRef.Layer; ar.ColorIndex = blockRef.ColorIndex; blockRef.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); } } } } private void RegisterRegAppName(Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { RegAppTable regAppTable = tr.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable; if (!regAppTable.Has(RegAppName)) { RegAppTableRecord record = new RegAppTableRecord(); record.Name = RegAppName; regAppTable.Add(record); tr.AddNewlyCreatedDBObject(record, true); } tr.Commit(); } } } } 整体代码实现还是有问题,我的需求如下: 1、在CAD界面先创建图形,然后把这些图形添加到块里 2、块添加完成之后,我需要输入命令选择苗木把苗木属性绑定到块的属性 3、绑定完成之后不需要输入其他命令,点击块,查看块的特性然后显示添加的苗木信息
07-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值