页面中的JSF错误 - 无法获取或设置bean的值

时间:2011-11-28 03:30:11

标签: jsf jsf-2

我将使用JSF进行Web应用程序,只是为了获取JSF中的值并将其放入bean中,反之亦然。我想我已经做好了一切,但当我启动服务器并尝试访问我的第一页时,我收到以下错误

SEVERE: Servlet.service() for servlet [jsp] in context with path [/SimpleJSF] threw exception [/greeting.jsp (line: 20, column: 85) #{...} is not allowed in template text] with root cause
org.apache.jasper.JasperException: /greeting.jsp (line: 20, column: 85) #{...} is not allowed in template text

我正在使用带有JDK 1.6的Eclipse Helios,apache Tomcat 7和JSF 2.0框架

这是我的代码段

greeting.jsp中

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
        <title>Guess Number Facelets Application</title>
    </h:head>
    <h:body>
        <h:form>

            <h2>
                Hi, my name is Duke. I am thinking of a number from
                0 to 10.
                Can you guess it?
            </h2>
            <p><h:inputText id="userNo" title="Type a number from 0 to 10:" value="#{resultNumber.userNumber}">
                    <f:validateLongRange minimum="#{resultNumber.minimum}" maximum="#{resultNumber.maximum}"/>
                </h:inputText>

                <h:commandButton id="submit" value="Submit" action="response.jsp"/>
            </p>
            <h:message showSummary="true" showDetail="false" style="color: #d20005; font-family: 'New Century Schoolbook', serif; font-style: oblique; text-decoration: overline"
                       id="errors1"
                       for="userNo"/>

        </h:form>
    </h:body>
</html>

的response.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
 <h:head>
        <title>Guess Number Facelets Application</title>
    </h:head>
    <h:body>
        <h:form>

            <h2>
                <h:outputText id="result" value="#{resultNumber.response}"/>
            </h2>
            <h:commandButton id="back" value="Back" action="greeting.xhtml"/>
        </h:form>
    </h:body>

</html>

Java bean, ResultNumber.java

package guessNumber;

import java.util.Random;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class ResultNumber
 */
@Stateless(mappedName = "resultNumber")
@LocalBean
public class ResultNumber {

     Integer randomInt = null;
        Integer userNumber = null;
        String response = null;
        private long maximum=10;
        private long minimum=0;

        public ResultNumber() {
            Random randomGR = new Random();
            randomInt = new Integer(randomGR.nextInt(10));
            System.out.println("Duke's number: " + randomInt);
        }

        public void setUserNumber(Integer user_number) {
            userNumber = user_number;
        }

        public Integer getUserNumber() {
            return userNumber;
        }

        public String getResponse() {
            if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {
                return "Yay! You got it!";
            } else {
                return "Sorry, " + userNumber + " is incorrect.";
            }
        }

        public long getMaximum() {
            return (this.maximum);
        }

        public void setMaximum(long maximum) {
            this.maximum = maximum;
        }

        public long getMinimum() {
            return (this.minimum);
        }

        public void setMinimum(long minimum) {
            this.minimum = minimum;
        }


}

现在是配置文件

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>SimpleJSF</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>faces/greeting.jsp</welcome-file>
  </welcome-file-list>
</web-app>

刻面的config.xml

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

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <managed-bean>
        <managed-bean-name>resultNumber</managed-bean-name>
        <managed-bean-class>guessNumber.ResultNumber</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>

2 个答案:

答案 0 :(得分:1)

在JSF 2.0中,JSP已被弃用为视图技术,并由Facelets取代。您需要将.jsp个文件重命名为.xhtml个文件。在您的情况下,您还需要删除两个JSP中的整个<%@ page %>行。然后,您需要使用网址中的.xhtml扩展名调用它们。

此外,您还需要从ConfigureListener中删除web.xml,并且需要将JSP欢迎文件重命名为XHTML。我还建议使用*.xhtml代替/faces/*作为FacesServlet网址格式。这样,您无需每次在网址中添加/faces/*。最后,您需要从faces-config.xml中删除bean,并按如下方式注释bean,而不是那些javax.ejb注释:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class ResultNumber {

毕竟,您似乎正在阅读JSF 1.x教程并将其与JSF 2.x混合使用。您应该非常小心JSF版本的JSF书籍/教程,您正在阅读目标。从JSF 2.0开始,很多事情都以不同的方式完成。

答案 1 :(得分:0)

您正在使用.jsp文件,因此您应手动导入taglib。在开始的html标记正上方添加这两行。

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>