Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @EnableAutoConfiguration
- @ComponentScan({"com.example"})
- public class Application extends SpringBootServletInitializer {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(Application.class, Initializer.class);
- }
- @Bean
- public ServletRegistrationBean servletRegistrationBean() {
- FacesServlet servlet = new FacesServlet();
- ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
- return servletRegistrationBean;
- }
- @Bean
- public FilterRegistrationBean rewriteFilter() {
- FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
- rwFilter.setDispatcherTypes(EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST,
- DispatcherType.ASYNC, DispatcherType.ERROR));
- rwFilter.addUrlPatterns("/*");
- return rwFilter;
- }
- @Bean
- public ServletRegistrationBean h2servletRegistration() {
- ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
- registration.addUrlMappings("/console/*");
- return registration;
- }
- }
- @RewriteConfiguration
- public class FacesRewriteConfigurationProvider extends HttpConfigurationProvider {
- @Override
- public int priority()
- {
- return 10;
- }
- @Override
- public Configuration getConfiguration(final ServletContext context)
- {
- return ConfigurationBuilder.begin()
- .addRule(Join.path("/").to("/index.jsf"))
- .addRule(Join.path("/index").to("/index.jsf"))
- .addRule(Join.path("/files").to("/file.jsf"))
- .addRule(Join.path("/register").to("/register.jsf"));
- }
- }
- @Configuration
- public class Initializer implements ServletContextInitializer {
- @Override
- public void onStartup(ServletContext servletContext) throws ServletException {
- System.err.println("------------------------------------");
- servletContext.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", "true");
- servletContext.setInitParameter("primefaces.THEME", "bootstrap");
- }
- }
- <h:form id="register" method="post">
- <h:message for="RegisterGroupPanel" style="color:red;" />
- <h:panelGrid columns="3" id="RegisterGroupPanel">
- <h:outputLabel for="username" value="Username : " />
- <h:inputText id="username" value="#{loginBean.username}" required="true"
- requiredMessage="Please enter username" />
- <h:message for="username" style="color: red;" />
- <h:outputLabel for="password" value="Password : " />
- <h:inputSecret id="password" value="#{loginBean.pwd}" required="true"
- requiredMessage="Please enter password" />
- <h:message for="password" style="color: red;" />
- </h:panelGrid>
- <h:commandButton action="#{loginBean.userLogin()}" value="Login" />
- </h:form>
- @ManagedBean
- @SessionScoped
- public class LoginBean {
- private String pwd;
- private String username;
- @ManagedProperty(value = "#{userRepository}")
- UserRepository userRepository;
- public String userLogin() throws IOException {
- User currentUser = userRepository.findByUsername(username);
- FacesContext context = FacesContext.getCurrentInstance();
- if (currentUser == null) {
- context.addMessage(null, new FacesMessage("username and password doesn't match, try again"));
- return null;
- } else {
- context.getExternalContext().getSessionMap().put("currentUser", currentUser);
- // context.getExternalContext().getSessionMap().put("testSession", "session working!!!");
- return "index.xhtml?faces-redirect=true";
- }
- }
- public String logout() {
- FacesContext context = FacesContext.getCurrentInstance();
- context.getExternalContext().invalidateSession();
- return "login.xhtml?faces-redirect=true";
- }
- }
- <application>
- <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
- </application>
- <servlet>
- <servlet-name>Faces Servlet</servlet-name>
- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Faces Servlet</servlet-name>
- <url-pattern>*.jsf</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
- <param-value>true</param-value>
- </context-param>
- <context-param>
- <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
- <param-value>0</param-value>
- </context-param>
- <context-param>
- <param-name>javax.faces.PROJECT_STAGE</param-name>
- <param-value>Development</param-value>
- </context-param>
- <context-param>
- <param-name>facelets.SKIP_COMMENTS</param-name>
- <param-value>true</param-value>
- </context-param>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement