表单提交结果显示的源代码是纯文本而不是被解释

时间:2012-10-08 17:02:19

标签: jsf facelets

我正在尝试创建一个登录页面,用于检查数据库中的用户名和密码。 一旦我点击提交按钮,它就会显示welcome.xhtml文件的代码,而不是显示欢迎页面。谁能告诉我如何解决这个问题。

在对login.xhtml进行更改以使用facelets后,我仍然遇到同样的问题。

login.xhtml:

<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">  

    <h:body>

      <h:form>

          <p>
              <h:inputText
                  id="username"
                  title="Username"
                  value="#{userBean.username}">

              <h:inputText
                  id="password"
                  title="Password"
                  value="#{userBean.password}">
              </h:inputText>

              <h:commandButton id="submit" value="Submit" action="#{userBean.validate}"/>
          </p>

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

托管bean:

@Stateless
public class userBean implements Serializable {

    private String username, password;
    private String response = "";
    private UserFacade userFacade;

    public userBean() {
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return username;
    }

    public void setUserName(String username) {
        this.username = username;
    }

    public String validate(){
      System.out.println("in bean");
        response = userFacade.validateUser(username, password);

      if (response.equals("MATCH"))          
          return "welcome.xhtml";
      else
          return "login.xhtml";

    }
}

1 个答案:

答案 0 :(得分:0)

您是否在.xhtml页面上指定了标签的xhtml声明和jsf声明?

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<h:body>
   your code here....
</h:body>

</html>
您在web.xml中的

是否指定了以下内容,

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
  </context-param>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
在托管bean中

尝试指定“faces /”

if (response.equals("MATCH"))          
      return "faces/welcome.xhtml";
  else
      return "login.xhtml";

在.xhtml登录文件中使用表单而不是<h:form>

<form>
</form>

尝试在faceConfig中指定导航规则

<navigation-rule>
  <display-name>login.xhtml</display-name>
  <from-view-id>/faces/login.xhtml</from-view-id>
  <navigation-case>
   <from-action>#{userBean.validate}</from-action>
   <from-outcome>welcome.xhtml</from-outcome>
   <to-view-id>/faces/welcome.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>

一些资源供您参考, http://docs.oracle.com/cd/E14545_01/help/org.eclipse.jst.jsf.facelet.doc.user/html/gettingstarted/tutorial/JSF%20Facelets%20Tools%20Tutorial.html