下面有个beanpackage com.ora.jsp.beans.userinfo;import java.io.*;
import java.util.*;
import com.ora.jsp.util.*;public class UserInfoBean implements Serializable {
// Validation constants
private static String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
private static String[] GENDER_LIST = {"m", "f"};
private static String[] FOOD_LIST = {"z", "p", "c"};
private static int MIN_LUCKY_NUMBER = 1;
private static int MAX_LUCKY_NUMBER = 100; // Properties
private String birthDate;
private String emailAddr;
private String[] food;
private String luckyNumber;
private String gender;
private String userName; /**
* Validates the gender property
*
* @return true if the property is set to a valid value, false otherwise.
*/
public boolean isUserNameValid() {
boolean isValid = false;
if (userName != null) {
isValid = true;
}
return isValid;
} 而有个jsp文件用到了这个bean,如下<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head>
<title>User Info Entry Form</title>
</head>
<body bgcolor="white">
<jsp:useBean id="userInfo"
class="com.ora.jsp.beans.userinfo.UserInfoBean">
<jsp:setProperty name="userInfo" property="*" />
</jsp:useBean> <form action="validate_bean.jsp" method="post">
<input type="hidden" name="submitted" value="true">
<table>
<c:if test="${param.submitted && userInfo.userNameValid == false}">
<tr><td></td>
<td colspan="2"><font color="red">
Please enter your Name
</font></td></tr>
</c:if>
<tr>
<td>Name:</td>
<td>
<input type="text" name="userName"
value="<c:out value="${userInfo.userName}" />">
</td>
</tr>
</table>
</form>
</body>
</html>搞不懂的就是下面这句
<c:if test="${param.submitted && userInfo.userNameValid == false}">
这里的userInfo的属性userNameValid是从哪里来的啊,
在那个bean里面只有isUserNameValid()这个方法 ,而没有userNameValid这个属性,
它们之间有什么关系呢?
import java.util.*;
import com.ora.jsp.util.*;public class UserInfoBean implements Serializable {
// Validation constants
private static String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
private static String[] GENDER_LIST = {"m", "f"};
private static String[] FOOD_LIST = {"z", "p", "c"};
private static int MIN_LUCKY_NUMBER = 1;
private static int MAX_LUCKY_NUMBER = 100; // Properties
private String birthDate;
private String emailAddr;
private String[] food;
private String luckyNumber;
private String gender;
private String userName; /**
* Validates the gender property
*
* @return true if the property is set to a valid value, false otherwise.
*/
public boolean isUserNameValid() {
boolean isValid = false;
if (userName != null) {
isValid = true;
}
return isValid;
} 而有个jsp文件用到了这个bean,如下<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head>
<title>User Info Entry Form</title>
</head>
<body bgcolor="white">
<jsp:useBean id="userInfo"
class="com.ora.jsp.beans.userinfo.UserInfoBean">
<jsp:setProperty name="userInfo" property="*" />
</jsp:useBean> <form action="validate_bean.jsp" method="post">
<input type="hidden" name="submitted" value="true">
<table>
<c:if test="${param.submitted && userInfo.userNameValid == false}">
<tr><td></td>
<td colspan="2"><font color="red">
Please enter your Name
</font></td></tr>
</c:if>
<tr>
<td>Name:</td>
<td>
<input type="text" name="userName"
value="<c:out value="${userInfo.userName}" />">
</td>
</tr>
</table>
</form>
</body>
</html>搞不懂的就是下面这句
<c:if test="${param.submitted && userInfo.userNameValid == false}">
这里的userInfo的属性userNameValid是从哪里来的啊,
在那个bean里面只有isUserNameValid()这个方法 ,而没有userNameValid这个属性,
它们之间有什么关系呢?
用userInfo.userNameValid时,系统会调用Bean对象的isUserNameValid()方法取值的。Bean有setProperty()、getProperty()或isProperty()(对于boolean型)方法时,Bean就有Property属性.
imA那个配置文件在哪里啊?是什么形式的呢?
还有xtaotao,你说的是javabean的一种默认规则吗,但是起码在bean里面应该有userNameValid才能有isUserNameValid方法呀真不知道该怎么办
我下面例子中:
域的名字:titleName
方法名称:setTitle和getTitle
因此我在JSP页面中可以访问的属性名称是title,而不是titleName。//TestBean.java---------------------------------------------------
package com.xl.abc;public class TestBean {
/** Creates a new instance of TestBean */
public TestBean() {
} private String titleName; /**
* 属性 title 的获取方法。
* @return 属性 title 的值。
*/
public String getTitle() { return this.titleName;
} /**
* 属性 title 的设置方法。
* @param title 属性 title 的新值。
*/
public void setTitle(String title) { this.titleName = title;
}
}
//testBean.jsp------------------------------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body> <h1>JSP Page</h1>
<jsp:useBean class="com.xl.abc.TestBean" id="aBean" scope="session">
</jsp:useBean>
<jsp:setProperty name="aBean" property="title" value="abcdedf" />
输出: ${aBean.title}
</body>
</html>