如何使用spring boot创建jsf应用程序

ncgqoxb0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(356)

我正在尝试创建一个jsfspring启动应用程序。这就是我的代码:
我的pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
  3. instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.4.0</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>tn.esprit.tp</groupId>
  13. <artifactId>tp.spring.mvc.rest</artifactId>
  14. <version>0.0.1</version>
  15. <name>tp-spring-mvc-rest</name>
  16. <description>TP Spring</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-jpa</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-devtools</artifactId>
  32. <scope>runtime</scope>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>mysql</groupId>
  37. <artifactId>mysql-connector-java</artifactId>
  38. <scope>runtime</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-test</artifactId>
  43. <scope>test</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.apache.myfaces.core</groupId>
  47. <artifactId>myfaces-impl</artifactId>
  48. <version>2.2.12</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.apache.myfaces.core</groupId>
  52. <artifactId>myfaces-api</artifactId>
  53. <version>2.2.12</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.apache.tomcat.embed</groupId>
  57. <artifactId>tomcat-embed-jasper</artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.ocpsoft.rewrite</groupId>
  61. <artifactId>rewrite-servlet</artifactId>
  62. <version>3.4.1.Final</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.ocpsoft.rewrite</groupId>
  66. <artifactId>rewrite-integration-faces</artifactId>
  67. <version>3.4.1.Final</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.primefaces</groupId>
  71. <artifactId>primefaces</artifactId>
  72. <version>6.1</version>
  73. </dependency>
  74. </dependencies>
  75. <build>
  76. <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
  77. <plugins>
  78. <plugin>
  79. <groupId>org.springframework.boot</groupId>
  80. <artifactId>spring-boot-maven-plugin</artifactId>
  81. </plugin>
  82. <plugin>
  83. <groupId>org.apache.maven.plugins</groupId>
  84. <artifactId>maven-surefire-plugin</artifactId>
  85. <version>2.19.1</version>
  86. </plugin>
  87. </plugins>
  88. </build>
  89. </project>

面配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <faces-config 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
  5. http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
  6. version="2.2">
  7. <application>
  8. <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  9. </application>
  10. </faces-config>

web.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
  3. <servlet>
  4. <servlet-name>Faces Servlet</servlet-name>
  5. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  6. <load-on-startup>1</load-on-startup>
  7. </servlet>
  8. <servlet-mapping>
  9. <servlet-name>Faces Servlet</servlet-name>
  10. <url-pattern>*.jsf</url-pattern>
  11. </servlet-mapping>
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <listener>
  16. <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  17. </listener>
  18. </web-app>

SpringBoot应用程序:

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. @ComponentScan({"tp.spring.mvc.rest"})
  4. public class TpSpringMvcRestApplication extends SpringBootServletInitializer implements CommandLineRunner{
  5. private static Logger LOGGER = Logger.getLogger(TpSpringMvcRestApplication.class.getName());
  6. @Autowired
  7. private UserService userService;
  8. public static void main(String[] args) {
  9. SpringApplication.run(TpSpringMvcRestApplication.class, args);
  10. }
  11. @Bean
  12. public ServletRegistrationBean servletRegistrationBean() {
  13. FacesServlet servlet = new FacesServlet();
  14. return new ServletRegistrationBean(servlet, "*.jsf");
  15. }
  16. @Bean
  17. public FilterRegistrationBean rewriteFilter() {
  18. FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
  19. rwFilter.setDispatcherTypes(
  20. EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR));
  21. rwFilter.addUrlPatterns("/*");
  22. return rwFilter;
  23. }

控制器:

  1. @Scope(value = "session")
  2. @Component(value = "userController") // Name of the bean in Spring IoC
  3. @ELBeanName(value = "userController") // Name of the bean used by JSF
  4. @Join(path = "/", to = "/login.jsf")
  5. public class UserController {
  6. public void doLogin() {
  7. System.out.println("HELLO");
  8. }
  9. }

xhtml登录页:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <ui:composition xmlns="http://www.w3.org/1999/xhtml"
  4. xmlns:f="http://java.sun.com/jsf/core"
  5. xmlns:h="http://java.sun.com/jsf/html"
  6. xmlns:ui="http://java.sun.com/jsf/facelets"
  7. template="/template/template.xhtml">
  8. <ui:param name="page_name" value="Connexion" />
  9. <ui:define name="menu">
  10. <h:form id="form">
  11. <b>Connexion</b>
  12. <h:panelGrid columns="2">
  13. <h:outputText value="Login (Email)" />
  14. <h:commandButton id="btn" value="Connexion"
  15. action="#{userController.doLogin()}" />
  16. </h:panelGrid>
  17. </h:form>
  18. </ui:define>
  19. </ui:composition>

问题是,当我单击connexion按钮时,controller中的方法没有被调用,我收到一个白标签错误页。
我如何解决这个问题?
先谢谢你

irtuqstp

irtuqstp1#

你能试着改变一下吗:

  1. return new ServletRegistrationBean(servlet, "*.xhtml");

相关问题