0

我在welcome.jsp 中有一个sx:autocompleter 标签,如下所示。虽然,自动完成器操作被调用,但结果没有正确呈现并且自动完成不起作用。(我在 WEB-INF/lib 中包含了 struts2-dojo-plugin-2.3.8.jar。)

(浏览器中的调试输出显示以下消息。

DEBUG:  please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints

问题末尾的详细信息。)

以下是我的代码。

欢迎.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%> 
................................
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<sx:head debug="true"/>
</head>
<body>
<s:form action="sub">
.........
<s:url var="fruitsurl" action="fruits"></s:url>
<sx:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}" list="fruitsList"></sx:autocompleter>
..........
<s:submit></s:submit>
</s:form>
</body>
</html> 

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
....................
<struts>

    <package name="some-default" extends="struts-default">

    <action name="fruits" class="mypack.FruitsAction" method="display">
         <result name="success">welcome.jsp</result>
    </action>

    <action name="sub" class="mypack.SubmitAction">
        <result name="success">/success.jsp</result>
    </action>   
    </package>
</struts>

FruitsAction.java

package mypack;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class FruitsAction extends ActionSupport{
    private List fruitsList;

    public List getFruitsList() {
        return fruitsList;
    }

    public void setFruitsList(List fruitsList) {
        this.fruitsList = fruitsList;
    }

    public String display() throws Exception {

        fruitsList = new ArrayList();

        fruitsList.add("apples");
        fruitsList.add("oranges");
        fruitsList.add("mangoes");

        return SUCCESS;
    }


}

我在 sx:head 中设置了 debug="true",当调用 welcome.jsp 时,浏览器中会显示以下 DEBUG 输出。

DEBUG:  please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints
DEBUG:  [SyntaxError: Syntax error]
DEBUG:<!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">
<title>Welcome</title><script language="JavaScript" type="text/javascript">
    // Dojo configuration    djConfig = {        isDebug: true,
        bindEncoding: "UTF-8"
          ,baseRelativePath: "/Suburbs/struts/dojo/"
          ,baseScriptUri: "/Suburbs/struts/dojo/"         ,parseWidgets : false
            };</script>  <script language="JavaScript" type="text/javascript"
        src="/Suburbs/struts/dojo/struts_dojo.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/Suburbs/struts/ajax/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript">
    dojo.hostenv.writeIncludes(true);</script>     
<link rel="stylesheet" href="/Suburbs/struts/xhtml/styles.css" type="text/css"/>
<script language="JavaScript" src="/Suburbs/struts/utils.js" type="text/javascript"></script>
<script language="JavaScript" src="/Suburbs/struts/xhtml/validation.js"   type="text/javascript"></script>
<script language="JavaScript" src="/Suburbs/struts/css_xhtml/validation.js" type="text/javascript"></script>
</head><body>
<form id="sub" name="sub" action="/Suburbs/sub.action" method="post">
<table class="wwFormTable"><tr>
    <td class="tdLabel"><label for="sub_autocompleter1" class="label">Fruits:</label></td>
    <td
>  <input dojoType="struts:ComboBox"  dataUrl="/Suburbs/fruits.action" id="sub_autocompleter1" name="autocompleter1" keyName="autocompleter1Key" visibleDownArrow="true" />
    <option value="apples">apples</option>
    <option value="oranges">oranges</option>
    <option value="mangoes">mangoes</option>  </select></td></tr>
<script language="JavaScript" type="text/javascript">djConfig.searchIds.push("sub_autocompleter1");</script>
<tr>
    <td colspan="2"><div align="right"><input type="submit" id="sub_0" value="Submit"/>
</div></td></tr></table></form></body></html>

请指导我了解代码可能有什么问题。

谢谢,

4

3 回答 3

2

您的操作需要返回 JSON,如Ajax 和 JavaScript 食谱页面中所述。(由于 wiki 导出问题,链接到以前的版本)。简而言之:使用 JSON 插件。

<action name="fruits" class="mypack.FruitsAction" method="display">
     <result type="json">
         <param name="root">fruits</param>
     </result>
</action>

请注意,我明确地将“根”元素设置为您的水果列表。我还消除了水果清单名称中的冗余;这是一个水果列表——它的名字应该是fruits

最后,出于各种原因,Dojo 插件已经被弃用至少几年了。如果您明确需要 Dojo,我强烈考虑使用类似Struts 2 jQuery Plugin或简单地使用原始 Dojo。

于 2013-07-29T12:57:35.520 回答
0

感谢Dave Newton,我对我的问题有以下解决方案。

我修改了代码以使用 JSON 和自动完成器,并将 struts2-json-plugin-2.3.8.jar 包含在 WEB-INF/lib 中。

在下面提供修改后的代码:

欢迎.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%> 
...........
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<sx:head/>
</head>
<body>
<s:form action="sub">
.......................
<s:url var="fruitsurl"  namespace="/autocompleter" action="getfruits"></s:url>
<sx:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}"></sx:autocompleter>
.......................
<s:submit></s:submit>
</s:form>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
............
<struts>

    <package name="autocompleter" namespace="/autocompleter" extends="json-default">
    <action name="getfruits" class="mypack.FruitsAction">
         <result type="json">
            <param name="root">fruits</param>
         </result>
    </action>
    </package>

    <package name="some-default" extends="struts-default">  
    <action name="sub" class="mypack.SubmitAction">
        <result name="success">/success.jsp</result>
    </action>   
    </package>
</struts>

FruitsAction.java

package mypack;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class FruitsAction extends ActionSupport{
    Map<String,String> fruits;

    public Map<String, String> getFruits() {
        fruits=new HashMap<String,String>();

        fruits.put("apples", "ap");
        fruits.put("oranges", "or");
        fruits.put("mangoes", "ma");
        return fruits;
    }

    public void setFruits(Map<String, String> fruits) {
        this.fruits = fruits;
    }   

}
于 2013-07-29T19:50:41.760 回答
0

以下是使用 jquery 插件而不是 dojo 插件的解决方案。(在 Eclipse 的 WEB-INF/lib 中用 struts2-jquery-plugin-3.6.0.jar 替换了 dojo 插件。)

欢迎.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> 
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%> 
..................
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title> 
<sj:head jqueryui="true"/>
</head>
<body>
<s:form action="sub">
.......................
<s:url var="fruitsurl"  namespace="/autocompleter" action="getfruits"></s:url>
<sj:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}"></sj:autocompleter>
......................
<s:submit></s:submit>
</s:form>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
................
<struts>

    <package name="autocompleter" namespace="/autocompleter" extends="json-default">
    <action name="getfruits" class="mypack.FruitsAction">
         <result type="json" name="success">
            <param name="root">fruitNames</param>
         </result>
    </action>
    </package>

    <package name="some-default" extends="struts-default">  
    <action name="sub" class="mypack.SubmitAction">
        <result name="success">/success.jsp</result>
    </action>   
    </package>
</struts>

FruitsAction.java

package mypack;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FruitsAction extends ActionSupport{
    private String term;

    List<String> fruitList=new ArrayList<String>();
    private String[] fruitNames;

    @Override
    public String execute() throws Exception {

        fruitList.add("apples");
        fruitList.add("mangoes");
        fruitList.add("pears");
        fruitList.add("grapes");

        if (StringUtils.isNotBlank(term)){
            ArrayList<String> subList=new ArrayList<String>();
            for (int i=0;i<fruitList.size();i++){
                if (StringUtils.startsWithIgnoreCase(fruitList.get(i), term)){
                    subList.add(fruitList.get(i));
                }
            }
            fruitNames=subList.toArray(new String[subList.size()]);
        }else{
            fruitNames=null;
        }

        return SUCCESS;
    }


    public String[] getFruitNames() {
        return fruitNames;
    }

    public void setTerm(String term) {
        this.term = term;
    }

}

在 FruitsAction.java 中,字符串 'term' 是我们在自动完成器中键入的内容。每次我们输入新字母时都会调用该操作。

我为上述解决方案引用了以下网址。

http://code.google.com/p/struts2-jquery/wiki/AutocompleterTag

http://www.coderanch.com/t/537518/Struts/struts-jquery-autocompleter

于 2013-07-31T20:31:47.047 回答