日期:2014-05-19  浏览次数:20890 次

SSH 初学之 HelloWorld
【问题】
如何不点击提交按钮,就能显示Server端的信息?

【环境】
eclipse7.5 JRE7 J2EE1.4 tomcat7

【struts.xml】
        <action name="HelloWorld2" class="helloWorld.HelloWorld2">
            <result name="SUCCESS">/HelloWorld2.jsp</result>
        </action>

【action】
public class HelloWorld2{

private String message;

public String getMessage() {
return message;
}

public String execute() throws Exception{
message = "HelloWorld2 from server";
return "SUCCESS";
}
}

【jsp】
<%@ page contentType="text/html;charset=GBK" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <title>Hello World2</title>
    </head>
    <body>
        <s:form action="HelloWorld2" >
    <h1><s:property value="message"/></h1>
<!--        <s:submit/>
 -->    </s:form>
    </body>
</html>

------最佳解决方案--------------------
我想说的是不提交是不可能实现的,只不过可以不用submit提交而已。
ajax的特点是局部刷新,这这个页面很明显需要的是全刷新,所以ajax是不适用的。
下面提供一种不使用submit按钮的方式。
<script type="text/javascript">
function checkForm(f){
//提交之前可以使用一些验证
f.submit();
}
</script>
<body>
<form name="myForm" action="UserRegisterServlet" method="post">
用户名:<input type="text" name="name" id="name" onblur="checkName(this.value)" /><span id="spanName"></span><br>
<img alt="" style="cursor: pointer" src="image/register.gif" onclick="checkForm(this.parentNode)">
 </form>
</body>
上面显示的按钮其实是一张图片。
------其他解决方案--------------------
如果不想点提交,可以用ajax来提交,也一样可以到后台操作
------其他解决方案--------------------
1.流程是:点提交按钮--》发送一个http请求----》服务端返回响应message到提交的页面
如果不点提交,就没有请求,
除非用服务器推送技术,服务器自动发送数据给客户端。
------其他解决方案--------------------
自己找到了一个简单方法:直接调后台的action。