《struts2权威指南》学习笔记之struts2 ajax标签之autocompleter

本文介绍Struts2框架中autocompleter组件的使用方法,包括配置web.xml和struts.xml文件,实现作者与书籍的联动选择功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

autocompleter 标签会生成一个带下拉按钮的单行文本输入框,当用户单击下拉按钮时,将看到一系列的选项,单击某个选项可以将该选项填入单行文本框

web.xml

 

<?xml version="1.0" encoding="GBK"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


        
<filter>
        
<filter-name>struts2</filter-name>
        
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>


    
<filter-mapping>
        
<filter-name>struts2</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>

</web-app>

 

 struts.xml

 

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>

    
<constant name="struts.custom.i18n.resources" value="messageResource"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>

    
<package name="ajax" extends="struts-default">
        
<action name="books">
            
<result>/data</result>
        
</action>
    
</package>

</struts>

 

JSON数据文件

[
 ["Spring2.0宝典"],
 ["轻量级J2EE企业实战"],
 ["基于J2EE的Ajax宝典"]
]

auto.jsp

 

<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    
<title>自动完成</title>
    
<s:head theme="ajax" debug="true"/>
</head>
将两个关联起来
<br/>
<body>
<form id="selectForm">
  请选择您喜欢的作者:
<br>
  
<s:autocompleter theme="simple" name="author" 
    list
="{'李刚','Rod Johnson' , 'David Flanagan'}"  
    value
="李刚" notifyTopics="/book"
    forceValidOption
="true"
    id
="sel"/>
</form>
请选择您喜欢的图书:
<br>
<s:url id="getBook" value="/getBook.action"/>
<s:autocompleter theme="ajax" href="${getBook}"  cssStyle="width: 240px;"
    autoComplete
="false" formId="selectForm" listenTopics="/book" forceValidOption="true" id="ops"/>
</body>
</html>

 

我们也可以为autocompleter做联动效果

首先编写action

 

package lee;

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

import com.opensymphony.xwork2.ActionSupport;

public class GetBookAction extends ActionSupport
{
    
private String author;
    
private List<String> books = new ArrayList<String>();

    
public String getAuthor()
    
{
        
return author;
    }


    
public void setAuthor(String author)
    
{
        
this.author = author;
    }


    
public List<String> getBooks()
    
{
        
return books;
    }


    
public String execute() throws Exception
    
{
        System.out.println(author);
        
if (author.equals("&#26446;&#21018;"))
        
{
            books.clear();
            books.add(
"Spring2.0宝典");
            books.add(
"轻量级J2EE企业应用实战");
            books.add(
"基于J2EE的Ajax宝典");
        }

        
else if (author.equals("Rod Johnson"))
        
{
            books.clear();
            books.add(
"Expert One-on-One J2EE Design and Development");
        }

        
else if (author.equals("David Flanagan"))
        
{
            books.clear();
            books.add(
"JavaScript权威指南");
        }

        
return SUCCESS;
    }


}

 

这里判断中文作者名时候使用的并不是gb2312的名字,而是unicode的,因为所填struts2使用的是dojo,而dojo使用的是unicode

更新struts.xml

 

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
    
<constant name="struts.custom.i18n.resources" value="messageResource"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>

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

        
<action name="getBook" class="lee.GetBookAction">
            
<result>/books.jsp</result>
        
</action>

    
</package>

</struts>

 

测试jsp

 

<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    
<title>自动完成</title>
    
<s:head theme="ajax" debug="true"/>
</head>
将两个关联起来
<br/>
<body>
<form id="selectForm">
  请选择您喜欢的作者:
<br>
  
<s:autocompleter theme="simple" name="author" 
    list
="{'李刚','Rod Johnson' , 'David Flanagan'}"  
    value
="李刚" notifyTopics="/book"
    forceValidOption
="true"
    id
="sel"/>
</form>
请选择您喜欢的图书:
<br>
<s:url id="getBook" value="/getBook.action"/>
<s:autocompleter theme="ajax" href="${getBook}"  cssStyle="width: 240px;"
    autoComplete
="false" formId="selectForm" listenTopics="/book" forceValidOption="true" id="ops"/>
</body>
</html>

 

book.jsp (用来动态生成联动输入框数据)

 

<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
[
<s:iterator value="books">
["
<s:property/>"],
</s:iterator>
]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值