Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. @EnableAutoConfiguration
  2. @ComponentScan({"com.example"})
  3. public class Application extends SpringBootServletInitializer {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. }
  8.  
  9. @Override
  10. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  11. return application.sources(Application.class, Initializer.class);
  12. }
  13.  
  14. @Bean
  15. public ServletRegistrationBean servletRegistrationBean() {
  16. FacesServlet servlet = new FacesServlet();
  17. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
  18. return servletRegistrationBean;
  19. }
  20.  
  21. @Bean
  22. public FilterRegistrationBean rewriteFilter() {
  23. FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
  24. rwFilter.setDispatcherTypes(EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST,
  25. DispatcherType.ASYNC, DispatcherType.ERROR));
  26. rwFilter.addUrlPatterns("/*");
  27. return rwFilter;
  28. }
  29. @Bean
  30. public ServletRegistrationBean h2servletRegistration() {
  31. ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
  32. registration.addUrlMappings("/console/*");
  33. return registration;
  34. }
  35. }
  36.  
  37. @RewriteConfiguration
  38. public class FacesRewriteConfigurationProvider extends HttpConfigurationProvider {
  39.  
  40. @Override
  41. public int priority()
  42. {
  43. return 10;
  44. }
  45.  
  46. @Override
  47. public Configuration getConfiguration(final ServletContext context)
  48. {
  49. return ConfigurationBuilder.begin()
  50. .addRule(Join.path("/").to("/index.jsf"))
  51. .addRule(Join.path("/index").to("/index.jsf"))
  52. .addRule(Join.path("/files").to("/file.jsf"))
  53. .addRule(Join.path("/register").to("/register.jsf"));
  54. }
  55. }
  56.  
  57. @Configuration
  58. public class Initializer implements ServletContextInitializer {
  59. @Override
  60. public void onStartup(ServletContext servletContext) throws ServletException {
  61. System.err.println("------------------------------------");
  62. servletContext.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", "true");
  63. servletContext.setInitParameter("primefaces.THEME", "bootstrap");
  64. }
  65. }
  66.  
  67. <h:form id="register" method="post">
  68. <h:message for="RegisterGroupPanel" style="color:red;" />
  69. <h:panelGrid columns="3" id="RegisterGroupPanel">
  70. <h:outputLabel for="username" value="Username : " />
  71. <h:inputText id="username" value="#{loginBean.username}" required="true"
  72. requiredMessage="Please enter username" />
  73. <h:message for="username" style="color: red;" />
  74.  
  75. <h:outputLabel for="password" value="Password : " />
  76. <h:inputSecret id="password" value="#{loginBean.pwd}" required="true"
  77. requiredMessage="Please enter password" />
  78. <h:message for="password" style="color: red;" />
  79. </h:panelGrid>
  80.  
  81. <h:commandButton action="#{loginBean.userLogin()}" value="Login" />
  82. </h:form>
  83.  
  84. @ManagedBean
  85. @SessionScoped
  86. public class LoginBean {
  87.  
  88. private String pwd;
  89. private String username;
  90.  
  91. @ManagedProperty(value = "#{userRepository}")
  92. UserRepository userRepository;
  93.  
  94. public String userLogin() throws IOException {
  95. User currentUser = userRepository.findByUsername(username);
  96. FacesContext context = FacesContext.getCurrentInstance();
  97.  
  98. if (currentUser == null) {
  99. context.addMessage(null, new FacesMessage("username and password doesn't match, try again"));
  100. return null;
  101. } else {
  102. context.getExternalContext().getSessionMap().put("currentUser", currentUser);
  103. // context.getExternalContext().getSessionMap().put("testSession", "session working!!!");
  104. return "index.xhtml?faces-redirect=true";
  105. }
  106. }
  107.  
  108. public String logout() {
  109. FacesContext context = FacesContext.getCurrentInstance();
  110. context.getExternalContext().invalidateSession();
  111. return "login.xhtml?faces-redirect=true";
  112. }
  113. }
  114.  
  115. <application>
  116. <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  117. </application>
  118.  
  119. <servlet>
  120. <servlet-name>Faces Servlet</servlet-name>
  121. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  122. <load-on-startup>1</load-on-startup>
  123. </servlet>
  124. <servlet-mapping>
  125. <servlet-name>Faces Servlet</servlet-name>
  126. <url-pattern>*.jsf</url-pattern>
  127. </servlet-mapping>
  128. <context-param>
  129. <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
  130. <param-value>true</param-value>
  131. </context-param>
  132. <context-param>
  133. <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
  134. <param-value>0</param-value>
  135. </context-param>
  136. <context-param>
  137. <param-name>javax.faces.PROJECT_STAGE</param-name>
  138. <param-value>Development</param-value>
  139. </context-param>
  140. <context-param>
  141. <param-name>facelets.SKIP_COMMENTS</param-name>
  142. <param-value>true</param-value>
  143. </context-param>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement