Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JSF + Bean + eigener Tag

Empfohlene Antworten

Veröffentlicht

Hi,

ich habe ein - hoffentlich kleines Problem - mit einer JSF Seite.

Ich habe mir dafür ein eigenes Tag definiert. Dazu habe ich die folgende Klasse geschrieben


public class Rating extends TagSupport {


	private float rating = 0.0f;

	private int max = 5;

	private String full = null;

	private String half = null;

	private String none = null;



	@Override

	public int doEndTag() throws JspException {

		try {

			JspWriter out = pageContext.getOut();

			int n = 0;

			for (int i = 0; i < (int) rating; i++ ) {

				out.write("<img src=\"" + full + "\" />");

				n++ ;

			}

			if ((rating - (int) rating) >= 0.5) {

				out.write("<img src=\"" + half + "\" />");

				n++ ;

			}

			while ( ++n <= max) {

				out.write("<img src=\"" + none + "\" />");

			}


		} catch (Exception e) {}

		return EVAL_PAGE;

	}


	// Getter und Setter

}

Dazu die passende TLD

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

<taglib 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-jsptaglibrary_2_0.xsd" version="2.0"> 

  <tlib-version>1.0</tlib-version>

  <short-name>jsp4</short-name>

  <uri>/intech/rating</uri>

  <tag>

  	<name>rating</name>

  	<tag-class>intech.Rating</tag-class>

  	<body-content>JSP</body-content>

  	<attribute>

  		<name>rating</name>

  		<required>true</required>

  		<rtexprvalue>true</rtexprvalue>

  	</attribute>

  	<attribute>

  		<name>max</name>

  		<required>false</required>

  		<rtexprvalue>true</rtexprvalue>

  	</attribute>

  	<attribute>

  		<name>full</name>

  		<required>true</required>

  		<rtexprvalue>true</rtexprvalue>

  	</attribute>

  	<attribute>

  		<name>half</name>

  		<required>true</required>

  		<rtexprvalue>true</rtexprvalue>

  	</attribute>

  	<attribute>

  		<name>none</name>

  		<required>true</required>

  		<rtexprvalue>true</rtexprvalue>

  	</attribute>

  </tag>

</taglib> 

Und die TLD in die web.xml eingebunden

  <jsp-config>

  	<taglib>

  		<taglib-uri>/intech/rating</taglib-uri>

  		<taglib-location>/WEB-INF/tlds/rating.tld</taglib-location>

  	</taglib>

  </jsp-config>

Das Tag soll eine Wertung in Form von vollen, halben und leeren Sterne anzeigen. Also: z.B. max = 5, rating = 3,5, full, half, none entsprechnd die URI zu den Bildern der halben, vollen, leeren Sterne würde drei volle Sterne, einen halben und einen leeren anzeigen. Das Tag nutze ich nun in einer JSF dataTable.

		<h:dataTable value="#{search.searchResults}" var="list" rowClasses="odd,even" styleClass="dataTable">

			<!-- ... -->

			<h:column>

				<f:facet name="header">

					<h:outputText value="Bewertung" />

				</f:facet>

				<i:rating rating="#{list.rating}" full="../img/fullstar.png" half="../img/halfstar.png" none="../img/nostar.png" />

			</h:column>

		</h:dataTable>


Als wert für das Attribut rating möchte ich den Wert in meiner Datenquelle nutzen. Ich habe dies ähnlich versucht, wie z.B. beim outputText, also #{list.rating}. Allerdings wird dieser Ausdruck nicht ausgewertet und ich bekomme einen java.lang.NumberFormatException: For input string: "#{list.rating}"

Die naheliegende Frage ist nun, was mache ich falsch?

Wenn ich anstatt #{list.rating} z.B. 3 einsetzte zeigt das Tag das gewünschte Verhalten.

Hi,

probier mal rating als String zu setzen und erst in der Klasse Rating in eine Zahl umzuwandeln.

Gruß Jaraz

Vielen Dank für die Antwort. Hab das ganzen noch ein wenig an die neue Version angepasst und läuft super.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.