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

发布于 2024-12-18 10:36:53 字数 6900 浏览 1 评论 0原文

我要用 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

我正在使用 Eclipse Helios 与 JDK 1.6 、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>

facet-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>

I am to do a web application with JSF, just to get and put a value from the JSF to bean and vice versa. I think I have done everything properly but when I start the server and try to access my first page I get the following error

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

I am using Eclipse Helios with JDK 1.6 , apache Tomcat 7 and JSF 2.0 framework

Here is my code snippet

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;
        }


}

Now for the configuration files

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>

facet-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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2024-12-25 10:36:53

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

此外,您还需要从 web.xml 中删除 ConfigureListener,并且需要将 JSP 欢迎文件重命名为 XHTML。我还建议使用 *.xhtml 而不是 /faces/* 作为 FacesServlet URL 模式。这样您就不需要每次都在 URL 中添加 /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 以来,很多事情都以不同的方式完成。

In JSF 2.0, JSP is been deprecated as view technology and succeeded by Facelets. You need to rename your .jsp files to .xhtml files. In your case you also need to remove the entire <%@ page %> line in both JSPs. Then you need to invoke them with the .xhtml extension in the URL.

Further, you also need to remove the ConfigureListener from web.xml and you need to rename the JSP welcome file to XHTML. I'd also suggest to use *.xhtml instead of /faces/* as FacesServlet URL pattern. This way you don't need to put a /faces/* in URLs everytime. Finally you need to remove the bean from faces-config.xml and annotate the bean as follows instead of those javax.ejb annotations:

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

@ManagedBean
@RequestScoped
public class ResultNumber {

After all, it seems that you were reading JSF 1.x tutorials and mixing it up with JSF 2.x. You should be extremely careful what JSF version the JSF book/tutorial you're reading target. Since JSF 2.0 a lot of things are done differently.

故事↓在人 2024-12-25 10:36:53

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

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

You are using .jsp files,so you should import the taglibs manually.Add these two lines right above your opening html tag.

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文