Use the Jstl tab to access the list and determine the selected item in the list

Source: Internet
Author: User
Tags foreach object access
js| Visit | visit | check

This article will tell you how to access the list object through the Sun Company's JSTL tag and display it on a JSP page

In general, the list object is stored in the request object, in the Session object, and the framework (for example, action in the Struts framework) is used to put the list object into the Request object.

xxaction{

Public Actionforward Execute (
Actionmapping Mapping,
Actionform form,
HttpServletRequest request,
HttpServletResponse response) {

* * Get user information from session

HttpSession session=request.getsession ();
User user = (user) Session.getattribute ("User");
Request.setattribute ("user", user);

/* Establish rolelist*/

List rolelist =new ArrayList ();
for (int i=0;i<5;i++) {
Role role= The new role (I, the "role" +i);
Rolelist.add (role);
}

Request.setattribute ("Role_list", rolelist);

}

This article focuses on the JSTL label section:

<%@ taglib uri= "/jstl/core" prefix= "C"%>

<c:set var= "user" value= "${user}"/>

<tr bgcolor= "#FFFFFF" >
<TD align= "center" valign= "Top" >

<!--

Because of the problem with Chinese characters, you use the Struts tab to read the Adduser.role item in the resource file, the corresponding encoding is: role-->

<bean:message key= "Adduser.role"/></td>
<td>
<select name= "Role" >
<c:foreach var= "ro" items= "${role_list}" ><!--access to the Role_list object in the request-->
<c:choose>
<c:when test= "${user.role==ro.role_name}" ><!--, equivalent to if (User.getrole (). Equlas (Rolgetrolename ()) If the role in the user is the same as the role in the list, select (highlight) in the Select-->

<option selected= "Selected"
Value= "<c:out value=" ${ro.role_id} "/>" ><!--Set the value item so that you see the name in the page, and when selected

After that, the receiver is actually id-->.
<c:out value= "${ro.role_name}"/><!--see role_name-->.
</option>
</c:when>
<c:otherwise><!--equivalent to else-->
<option value= "<c:out value=" ${ro.role_id} "/>" >< principle of!---ditto-->
<c:out
Value= "${ro.role_name}" ></c:out></option>
</c:otherwise>
</c:choose>
</c:forEach><!--Cycle end-->

</select></td>
</tr>

Mutual encouragement, welcome Exchange:wyf_cjl@sohu.com



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.