【问题标题】:JSF error in the page - not able get or set values to the bean页面中的 JSF 错误 - 无法获取或设置 bean 的值
【发布时间】:2011-11-28 03:30:11
【问题描述】:

我要使用 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 框架一起使用

这是我的代码 sn-p

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>

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    在 JSF 2.0 中,JSP 作为视图技术已被弃用,并由 Facelets 接替。您需要将 .jsp 文件重命名为 .xhtml 文件。在您的情况下,您还需要删除两个 JSP 中的整个 &lt;%@ page %&gt; 行。然后您需要在 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 开始,很多事情都做了不同的处理。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-08-17
        • 2010-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 2011-04-16
        相关资源
        最近更新 更多