今天用struts2的action转向页面时,发现action那个类会运行两遍。经过漫长的寻找,发现了在页面上如果多了<img src="">这行代码action就会多运行遍。我以为是我自己项目的问题,我就再新建个还是一样,就连struts2的版本换了都有这个问题。求人解答,PS你们可以自己试下

解决方案 »

  1.   

    可能我表述问题,就是在转向页面时,它action那个方法会运行两遍,我查到了是在转向到的那个页面多了一行<img src="">,如果去掉就只一遍。
      

  2.   

    我也没有听明白。
    楼主如果觉得哪里有问题,可以把配置和代码发出来。
    这样我们也可以帮你定位问题。但是我想img的src本身就是去服务器获取信息,但是这肯定要服务器响应成功之后,由浏览器单独发起的请求。
    如果按楼主所说会因为img而导致某个方法多次执行,实属不可思议。
      

  3.   

    以下是我测试的代码
    struts.xml<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="default" namespace="/" extends="struts-default">
    <action name="test" class="test.TestAction">
    <result name="success">/a.jsp</result>
    </action>
    </package></struts>web.xml<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>TestActionpackage test;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport { private static final long serialVersionUID = -5088340972147296768L; @Override
    public String execute() throws Exception {
    System.out.println("这是测试");
    return SUCCESS;
    }}
    a.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
    <img src="" />
    </body>
    </html>
    就是这样,我在url输入test.action然后它转到a.jsp,我在控制台就看到了输出两次的“这是测试”