开发者

Displaying Java Property in JSP

I have a class with a property called title and I have a getter/setter that gets and sets the property. If the property is P I need to print the word "Peer" on the page, and if it's T I need to print "Team" on the page. Can I do this in a JSP without using a scriplets? I tried using

<jsp:getProperty name="value" class"classname"  />

but from there I have no idea how to use a开发者_如何学运维 conditional in a JSP. Please help.


Use JSTL, as @CoolBeans says. It would look something like this:

In the servlet,

// where myBean is an instance of the class with [get|set]Title
request.setAttribute("myFoo", myBean);

Then, in the JSP,

<c:choose>
    <c:when test="${myBean.title eq 'P'}">Peer</c:when>
    <c:when test="${myBean.title eq 'T'}">Team</c:when>
</c:choose>

If you're not familiar with JSTL, I'd recommend reading through the JSP section of the Java EE 5 Tutorial, or picking up a copy of Head First Servlets and JSP (it's quite good).


You should use JSTL. Here is an example:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>


  <c:if test="${yourClass.p eq 'P'}">PEER</c:if>


I used properties to set and get data from the user, by creating a java class handler and use a "useBean"... Here is some code I generated to fix my problem... hope it helps..

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login and Do Action</title>
</head>
<% // java code


%>
<body>
    <jsp:useBean id="myBean" scope="session" class="org.mypackage.IFPWAFCAD.NameHandler" />
    <jsp:setProperty name="myBean" property="name"/>
    <jsp:setProperty name="myBean" property="screenName"/>
    <jsp:setProperty name="myBean" property="username"/>
    <jsp:setProperty name="myBean" property="password" />



    <h1>Hello <jsp:getProperty name="myBean" property="screenName" />! Ready to Login and perform Action</h1>

    <form method="post" action="DBConnection"> 

        <table border="0">
            <thead>
                <tr>
                    <th>
                    </th>
                    <th>    
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Table:</td>
                    <td><input type="radio" name="tableName" value="product"/>Produce
                        <<input type="radio" name="tableName" value="customer" />Customer
                        <input type="radio" name="tableName" value="actor"  checked="checked" />Actor</td>

                </tr>
                <tr>
                    <td>Action:</td>
                    <td>
                        <select name="action" value="0">
                            <option value="0">Choose a Action...</option>
                            <option value="create">Create</option>
                            <option value="read">Read</option>
                            <option value="update">Update</option>
                            <option value="delete">Delete</option>
                            <option value="fancy">Fancy Display</option>
                            <option value="pass">Pass to JSP File</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Record ID:</td>
                    <td><input type="text" name="tid" size="3"/></td>
                </tr>
                <tr>
                    <td>
                        First Name:
                    </td>
                    <td>
                        <input type="text" name="firstname" size="30"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        Last Name:
                    </td>
                    <td>
                        <input type="text" name="lastname" size="30"/>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <input type="hidden" name="name" value="<jsp:getProperty name="myBean" property="name" />" />
                        <input type="hidden" name="username" value="<jsp:getProperty name="myBean" property="username" />" />
                        <input type="hidden" name="screenName" value="<jsp:getProperty name="myBean" property="screenName" />" />
                        <input type="hidden" name="password" value="<jsp:getProperty name="myBean" property="password" />" />
                    </td>
                </tr>

            </tbody>
        </table>
        <input type="submit" value="Login" />
        <input type="reset" name="clear" value="Clear" />

    </form>
</body>

Displaying Java Property in JSP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜