@named vs managedbean注解问题8

xghobddn  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(331)

我在做从甲骨文jee8教程,有一个部分的问题。https://javaee.github.io/tutorial/webapp003.html -在这里您可以找到原始代码。
问题在于 @Named 提交后出现错误的注解: /index.xhtml @20,43 value="#{hello.name}": Target Unreachable, identifier 'hello' resolved to null 但如果我用弃用的 @ManagedBean(name = "hello") 会有用的。
我哪儿也找不到为什么?即使在这里我也读了一些文章,但没有人回答我的问题。有人能解释一下为什么会这样吗?我做错了什么?
堆栈为jee8、maven 3.6.3、glassfish 5.0.1
这是我唯一的豆子。但如果我取消注解 @Named 和评论 @ManagedBean 它将停止工作,我得到提交后的错误。

  1. package hello1;
  2. import javax.enterprise.context.RequestScoped;
  3. import javax.faces.bean.ManagedBean;
  4. import javax.inject.Named;
  5. @ManagedBean(name = "hello")
  6. /*@Named*/
  7. @RequestScoped
  8. public class Hello {
  9. private String name;
  10. public Hello() {}
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String user_name) {
  15. this.name = user_name;
  16. }
  17. }

这是我的*.xhtml文件index.xhtml

  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html lang="en"
  5. xmlns="http://www.w3.org/1999/xhtml"
  6. xmlns:h="http://xmlns.jcp.org/jsf/html">
  7. <h:head>
  8. <title>Facelets Hello Greeting</title>
  9. </h:head>
  10. <h:body>
  11. <h:form>
  12. <h:graphicImage url="#{resource['images:duke.waving.gif']}"
  13. alt="Duke waving his hand"/>
  14. <h2>Hello, my name is Duke. What's yours?</h2>
  15. <h:inputText id="username"
  16. title="My name is: "
  17. value="#{hello.name}"
  18. required="true"
  19. requiredMessage="Error: A name is required."
  20. maxlength="25" />
  21. <p></p>
  22. <h:commandButton id="submit" value="Submit"
  23. action="response" />
  24. <h:commandButton id="reset" value="Reset" type="reset" />
  25. </h:form>
  26. <div class="messagecolor">
  27. <h:messages showSummary="true"
  28. showDetail="false"
  29. errorStyle="color: #d20005"
  30. infoStyle="color: blue"/>
  31. </div>
  32. </h:body>
  33. </html>

响应.xhtml

  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html lang="en"
  5. xmlns="http://www.w3.org/1999/xhtml"
  6. xmlns:h="http://xmlns.jcp.org/jsf/html">
  7. <h:head>
  8. <title>Facelets Hello Response</title>
  9. </h:head>
  10. <h:body>
  11. <h:form>
  12. <h:graphicImage url="#{resource['images:duke.waving.gif']}"
  13. alt="Duke waving his hand"/>
  14. <h2>Hello, #{hello.name}!</h2>
  15. <p></p>
  16. <h:commandButton id="back" value="Back" action="index" />
  17. </h:form>
  18. </h:body>
  19. </html>

web.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <context-param>
  7. <param-name>javax.faces.PROJECT_STAGE</param-name>
  8. <param-value>Development</param-value>
  9. </context-param>
  10. <servlet>
  11. <servlet-name>Faces Servlet</servlet-name>
  12. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>Faces Servlet</servlet-name>
  17. <url-pattern>*.xhtml</url-pattern>
  18. </servlet-mapping>
  19. <session-config>
  20. <session-timeout>
  21. 30
  22. </session-timeout>
  23. </session-config>
  24. <welcome-file-list>
  25. <welcome-file>index.xhtml</welcome-file>
  26. </welcome-file-list>
  27. </web-app>

pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>hello1-custom</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>hello1-custom</name>
  10. <packaging>war</packaging>
  11. <properties>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <junit.version>5.7.0</junit.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>javax</groupId>
  19. <artifactId>javaee-api</artifactId>
  20. <version>8.0.1</version>
  21. <scope>provided</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.junit.jupiter</groupId>
  25. <artifactId>junit-jupiter-api</artifactId>
  26. <version>${junit.version}</version>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.junit.jupiter</groupId>
  31. <artifactId>junit-jupiter-engine</artifactId>
  32. <version>${junit.version}</version>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-war-plugin</artifactId>
  41. <version>3.3.0</version>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题