gpt4 book ai didi

java - 无法在 JBOSS4.2 上部署和运行 JSF2.1.6 示例

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:26 25 4
gpt4 key购买 nike

web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">

<!-- <context-param>
<description />
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>-->
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<context-param>
<description/>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<context-param>
<param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
<param-value>true</param-value>
</context-param>

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>

<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>addUser.jsp</welcome-file>
</welcome-file-list>
</web-app>

faces-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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.test.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>AddUser</display-name>
<from-view-id>/AddUser.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/ListUser.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

UserBean 类

  package com.test;

public class UserBean{

private int id;
private String name;

//Action method to add user
public String addUser() {

return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

addUser.jsp

  <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>Add New User Form</title>
</head>
<body>
<f:view>
<p>
<h:message id="errors" for="User_Id"></h:message>
</p>
<h:form>
<h:panelGrid border="1" columns="2">
<h:outputText value="ID"></h:outputText>
<h:inputText id="User_ID" value="#{userBean.id}" required="true">
<f:validateLongRange minimum="1" maximum="500"></f:validateLongRange>
</h:inputText>

<h:outputText value="Name"></h:outputText>
<h:inputText value="#{userBean.name}"></h:inputText>
<h:commandButton action="#{userBean.addUser}" value="Add Customer"> </h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</html>

ListUser.jsp

   <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>List of Users</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="User #{userBean.name} is added succesfully"> </h:outputText>
</h:form>

</f:view>
</body>
</html>

这是从以下链接完成的示例

http://viralpatel.net/blogs/tutorial-creating-javaserver-faces-jsf-application-in-eclipse-jsf-project-jsf-tutorial/

但有些我无法将其部署在 jboss 上。

最佳答案

您正在使用 JSF 2.1 实现,但已将 faces-config.xml 文件配置为旧的 JSF 1.2:

<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_1_2.xsd"
version="1.2">
^here's the main problem

只需将 faces-config.xml 文件更新为正确的 JSF 版本即可:

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

或者更好的是,如果您要使用任何特定配置(例如注册阶段监听器),只需将其删除并添加即可。您可以在此处找到此文件的更多用例:What is the use of faces-config.xml in JSF 2? .

除此之外,还有其他主要问题:

  • JSF 2.x 至少需要 Servlet 2.5,该版本自 JBoss 5 起可用。请参阅 StackOverflow JSF wiki有关此主题的更多信息。
  • 您当前正在学习的教程涉及 JSF 1.2,它已经很旧了。 JSF 2.0 自 2009 年以来已发布。我建议停止遵循本教程并转向更新的教程,如 mkyong's tutorial on JSF 2 .

如果您确实想坚持使用 JSF 1.2 来满足特定要求(例如维护遗留应用程序),请将 JSF 库从 2.1 替换为 1.2。

关于java - 无法在 JBOSS4.2 上部署和运行 JSF2.1.6 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com