本文介紹了如何從struts 2 中的java 腳本重定向到struts 動作?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從 java 腳本重定向到 struts 動作?
how to redirect to struts action from java script?
如果腳本中的條件成功,那么我需要在配置 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
<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" "http://www.w3.org/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 動作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!