如何从struts 2中的java脚本重定向到struts动作?

编程入门 行业动态 更新时间:2024-10-25 03:24:14
本文介绍了如何从struts 2中的java脚本重定向到struts动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何从java脚本重定向到struts动作?

how to redirect to struts action from java script?

如果脚本中的条件成功,那么我需要在config xml中调用一个动作,否则没有动作调用控件需要保持同一页面?请帮忙吗?

if condition in the script got success then i need to invoke one action in the config xml, otherwise no action invoking the control needs to stay at same page? please help on this?

Java脚本

function displayDate() { var x=document.getElementsByName("userName") if(x = "shan") { alert("shankarasd"); document.myForm.action ="/setUpForInsertOrUpdate"; document.myForm.submit(); } }

HTML

<html> <body> <s:form action="HelloWorld" > <s:textfield name="userName" label="User Name" /> <s:submit onclick="displayDate()" /> </s:form> </body> </html>

config.xml

config.xml

<struts> <package name="default" extends="struts-default"> <action name="HelloWorld" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS">/success.jsp</result> </action> <action name="setUpForInsertOrUpdate" method="setUpForInsertOrUpdate" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS1">/success1.jsp</result> </action> <action name="back" method="back" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS2">/success.jsp</result> </action> </package> </struts>

推荐答案

试试

window.location='youractionname'

这会将您的窗口重定向到您的目的地

This will redirect your window into your destination

如果您想将您的值发送到其他页面,请按照此方法

If you want to sent your values to other page, then follow this method

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript"> function displayDate() { var x=document.getElementsByName("userName") if(x =="shan") { alert("Redirecting"); return true; } else{ alert("Not Redirecting"); return false; } } </script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:form action="HelloWorld" onsubmit="displayDate();"> <s:textfield name="userName" label="User Name" /> <s:submit onclick="displayDate()" /> </s:form> </body> </html>

我修改的是,将函数调用从按钮更改为表单提交。 并从函数中删除了不需要的东西

What I modified is, changed the function calling from button to form submit. And removed the unwanted things from function

更多推荐

如何从struts 2中的java脚本重定向到struts动作?

本文发布于:2023-10-10 22:40:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479959.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   重定向   动作   struts   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!