gpt4 book ai didi

java - 如何在struts2中使用dojo将表单数据提交给action在同一个jsp页面中显示结果

转载 作者:行者123 更新时间:2023-12-01 11:16:52 24 4
gpt4 key购买 nike

我正在尝试提交表单并在同一页面中显示输出。我正在使用 struts2 及其 dojo 插件来解决我的需求。我能够在同一页面中显示结果,但在按钮单击操作发生后,该操作会再次自动调用。因此,它将覆盖我刚刚从提交表单中获得的输出。

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<sx:head/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function enqDetails(){
alert("ok");
//document.firstForm.action = 'firstAction';
//document.firstForm.submit();
<s:url id="urlTestAction" action="firstAction"/>
dojo.event.topic.publish("show_detail");
document.getElementById('sxDivId1').style.display = 'block';
}
</script>
</head>
<body>
<h1> this is index page!!</h1>
<s:form id="firstForm" name="firstForm">
<s:textfield name="username" label="enter username"/>
<s:submit onclick="enqDetails()"></s:submit>
</s:form>
<sx:div id="sxDivId1" href="%{urlTestAction}" listenTopics="show_detail" formId="firstForm" loadingText="Please Wait...." showLoadingText="true" cssStyle="display:none;">
</sx:div>

</body>
</html>

struts.xml

  <action name="firstAction" class="action.FirstAction" method="first">
<result name="success">/second.jsp</result>
<result name="fail">/third.jsp</result>
</action>
</package>

FirstAction.jsp

package action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FirstAction extends ActionSupport{

private String username;

public String first(){
System.out.println("the username is :"+username);
if(username.equals("a")){
return "success";
}else{
return "fail";
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

控制台中显示以下内容。在再次使用输入的文本字段调用该操作后,该操作将使用空值调用,从而覆盖我的输出。

输出

the username is :a
Jul 30, 2015 12:19:36 PM org.apache.struts2.components.ServletUrlRenderer warn
WARNING: No configuration found for the specified action: '/' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
Jul 30, 2015 12:19:36 PM org.apache.struts2.components.ServletUrlRenderer warn
WARNING: No configuration found for the specified action: '/' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

最佳答案

struts2-jquery 插件 是我的答案。感谢大家的支持。

对于答案,请在索引中进行以下更改。 jsp就是这样。

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sj:head jquerytheme="start" jqueryui="true"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> this is index page!!</h1>

<s:form action="firstAction" id="nform" name="nform">
<s:textfield name="username" id="username" label="Enter ur name"/>
<sj:submit button="true" targets="ndiv"/>
</s:form>

<div id="ndiv">
initial data
</div>

</body>
</html>

关于java - 如何在struts2中使用dojo将表单数据提交给action在同一个jsp页面中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com