当前位置: 代码迷 >> Java Web开发 >> 这个ACTION为何刚进入页面执行了?代码贴出
  详细解决方案

这个ACTION为何刚进入页面执行了?代码贴出

热度:578   发布时间:2016-04-10 22:33:10.0
这个ACTION为什么刚进入页面执行了?代码贴出
jsp文件,为什么刚进这个页面的时候,就会去执行红色部分的“imageAction”ACTION啊?这个标签是自动提交的么?

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<script type="text/javascript">
function shuaxin(obj) {
var shijian = new Date().getTime();
obj.src = "imageAction.action?d=" + shijian;
}
</script>
</head>

<body>

<h1 align="center">
用户注册
</h1>
<div align="center">
<s:fielderror cssStyle="color:red"></s:fielderror>
<s:actionerror cssStyle="color:red"></s:actionerror>
<br>
<s:form action="register" theme="simple" method="post">
<table width="50%" border="0" style="width: 501px; height: 342px;">
<tr>
<td align="right">
账号:
</td>

<td width=20>
<s:textfield name="username"></s:textfield>
</td>

<td>
<font color="red" align="left">
<h6>
*必填
</h6> </font>
</td>
</tr>

<tr>
<td align="right">
密码:
</td>
<td>
<s:password name="password"></s:password>
</td>

<td>
<font color="red" align="left"><h6>
*必填
</h6> </font>
</td>
</tr>



<tr>
<td align="right">
重复密码:
</td>
<td>
<s:password name="repassword"></s:password>
</td>
<td>
<font color="red" align="left"><h6>
*必填
</h6> </font>
</td>
</tr>

<tr>
<td align="right">
性别:
</td>
<td>
<input type="radio" name="sex" value="0" checked />

<input type="radio" name="sex" value="1" />

</td>
</tr>

<tr>
<td align="right">
电子邮箱:
</td>

<td>
<s:textfield name="email"></s:textfield>
</td>

<td>
<font color="red" align="left"><h6>
*必填
</h6> </font>
</td>
</tr>
<tr>
<td align="right">
验证码:
</td>
<td>
<s:textfield name="validate"></s:textfield>
</td>
<td>
<img src="imageAction" align="left" alt="点击图片"
onClick="shuaxin(this)" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<s:submit value="提交"></s:submit>
<s:reset value="重置"></s:reset>
</td>
</tr>
</table>
</s:form>
</div>
</body>
</html>



struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant>

<package name="Struts2login" extends="struts-default" namespace="/">
<action name="register" class="action.RegisterAction">
<result name="success">/register_success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="login" class="action.LoginAction">
<result name="success" type="redirect">/login_success.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="imageAction" class="action.ImageAction">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>

</struts> 
------解决方案--------------------
肯定先执行红色的action啊。你图片不是指定了请求的url吗。当页面加载图片时就会去发请求获得图片

当然会去执行你红色部分的action
------解决方案--------------------
<img src="imageAction" align="left" alt="点击图片" onClick="shuaxin(this)" />
     ~~~~~~~~~~~~~~~~~
     问题处在这里,浏览器发现img标签后,会再次发出一个request请求,请求地址就是src指定的url
  相关解决方案