Struts 2.0 <s:select> solution of problems

Struts 2.0 <s:select> solution of problems

Today, the use of Struts 2.0 <s:select> encountered an error when the label:

tag 'select', field 'list', name 'university': The requested list key 'universities' could not be resolved as a collection / array / map / enumeration / iterator type.

google, baidu to find a lot of ways, all can not be solved, then finally found where the problem lies, in fact, is a small problem, to sum up here, so that we will appear similar mistakes in the future.
<s:select> is a drop-down list of labels, there are a variety of uses, such as direct specify a list of its assignment:

<s:select name="university" headerKey="-1" headerValue=" Please select the " list="{' South China University of technology ',' Huazhong University ',' Fuzhou University '}"/>

Sometimes, we need to get from the database inside the so-called "data dictionary", fill the drop-down list,

Let's look at a specific code it:

register.jsp part of the code snippet:

[Copy to clipboard] [-] CODE:
<form action="register.action" method="POST">
<s:select name="university" headerKey="-1" headerValue=" Please select the " list="universities" listKey="uniId" listValue="uniName"/>
</ form>
list attribute specifies a variable universities, this variable is a register of this action a member of the property, its type is List <University>:

[Copy to clipboard] [-] CODE:
... ... ... ...

public class Register extends ActionSupport (
private List <University> universities;

public List <University> getUniversities () (
return universities;
)

public void setUniversity (String university) (
this.university = university;
)

@ Override
public String execute () throws Exception (
universities = uniService.getUniversityList ();
... ... ... ...
)
)
Omitted here, a lot of things, but to illustrate the usage of select tag, uniService.getUniversityList () can be obtained from the database a list of a University, where omitted uniService statements and AutoWire. Of course, in addition to use List, you can also use Map.

struts.xml and Spring configuration file to register the action is mapped to Register.java. Specifically how the integration of Struts 2.0, Spring and Hibernate, a lot of online tutorials, you can also refer to Netbeans under:

http://developers.sun.com.cn/blog/functionalca/category/JavaEE

Omitted here for details.

If we run directly register.jsp, will receive the error described in the beginning we have, in fact, the reason is very simple, because for the first time when running the page, Register of this action has not been instantiated, so there is no universities in this variable. Then how should we do? Is very simple, first of all, in the register.jsp preceded by a page, for example index.jsp:

[Copy to clipboard] [-] CODE:
<s:url action="register">
<s:param name="type"> getForm </ s: param>
</ s: url>
<s:a href="%{regForm}"> Register </ s: a>
This page which is very simple, defines a url, the url will be submitted to register the action, and then define a hyperlink pointing to this url. Thus, when the user clicks the hyperlink time, action will be instantiated, universities can also get this variable. What type of this attribute is the use? Let's look at the action we need to do some changes to the execute method:

[Copy to clipboard] [-] CODE:
public String execute () throws Exception (
if (type! = null & & type.equals ( "getForm")) (
universities = uniService.getUniversityList ();
return INPUT;
)

if (type! = null & & type.equals ( "submit")) (
... ... ... ...
)
)
As we will once again of the form may be submitted (such as registration), it is important to distinguish is to initialize the page, or to submit the page. Using an attribute to differentiate on it. Of course, we also need to register.jsp which declared such a property, or when the form is submitted once again this variable did not get the:

[Copy to clipboard] [-] CODE:
<input type="hidden" name="type" value="submit"/>
Finally, run index.jsp, click on the hyperlink, the page will be able to show properly.

分类:Java 时间:2010-03-29 人气:726
分享到:
blog comments powered by Disqus

相关文章

  • Dynamic programming largest public String 2010-11-16

    Dynamic programming ----- seeking maximum public string two strings package zju; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SameString { public static void main(String[] args){ String s1=

  • java source code of the String 2010-12-07

    1.String Source Squadron String class description: The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be chang

  • Code is one of bad taste (the code embedded everywhere, string) 2010-11-15

    Code is one of bad taste (the code embedded everywhere, string) Look at the following piece of code: String type = form.getString("type","XZ"); //....... if("XZ".equals(type)){ ActionContext.setRequestAttribute("applyTyp

  • tcl [string map] is to facilitate the replacement string is 2011-04-12

    [Keywords]: Tcl string map replacement string Wrote string map?-nocase? mapping string Replaces substrings in string based on the key-value pairs in mapping. Mapping is a list of key value key value ... as in the form returned by array get. Each inst

  • tcl [string map] it is convenient to replace the string 2011-04-12

    [Keywords]: Tcl string map replacement string Wrote string map?-nocase? mapping string Replaces substrings in string based on the key-value pairs in mapping. Mapping is a list of key value key value ... as in the form returned by array get. Each inst

  • Java to send HTTP requests and extract data of Java code: 2010-08-02

    Java to send HTTP requests and extract data of Java code: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class Main { public static void main(String[] args) { try { URL my_url = new URL("http://www.xasxt.

  • ThinkPHP mechanism for dealing with massive data points more code table 2011-04-07

    ThinkPHP built-in table algorithm of mega-user data . Data table : house_member_0 house_member_1 house_member_2 house_member_3 In the model class MemberModel extends AdvModel { protected $partition = array('field'=>'username','type'=>'id','num'=>

  • Speak several Baidu provides an invitation code 2010-09-26

    Speak several Baidu provides an invitation code Request Address: http://www.028baidu.com/article.asp?id=130

  • Public String substring with maximum similarity String Process (2) 2010-04-06

    Most common sub-string: 2009-11-27 10:421. Levenshtein Distance The algorithm is also known as "edit distance", used to calculate the similarity of two strings. The principle is very simple, it is returned to the first string (delete, insert, re

  • Order to help a friend sort some data (a few code + sql) 2010-10-18

    Friends a little more chaos in the data files themselves, then manually adjust a little bit more complicated, people lazy, call me to help do this: I started and I thought it was always seen as telecommunications, where the data, a fairly standard an

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.382 (s). 12 q(s)