Showing posts with label jsf2. Show all posts
Showing posts with label jsf2. Show all posts

Wednesday, March 19, 2014

JSF 2.2 Spring 4.X Configuration

JSF 2.2 with Spring 4.X


File : web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>  
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <!-- Configuration locations must consist of one or more comma- or space-delimited
        fully-qualified @Configuration classes. Fully-qualified packages may also be
        specified for component-scanning -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.org.config.AppConfig</param-value>
    </context-param>
    <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
    <context-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>AppUserServlet</servlet-name>
        <servlet-class>com.org.servlet.AppUserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AppUserServlet</servlet-name>
        <url-pattern>/AppUserServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

</web-app>




Initialize Web Config


import org.springframework.security.web.context.*;
public class SecurityWebApplicationInitializer
      extends AbstractSecurityWebApplicationInitializer {
    public SecurityWebApplicationInitializer() {
        super(WebSecurityConfig.class);
    }
}






import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 @Override
 protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
  authManagerBuilder
   .inMemoryAuthentication().withUser("test").password("test").roles("ADMIN");

    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/faces/login.xhtml")
                .permitAll();
    }
    //http://www.petrikainulainen.net/programming/spring-framework/adding-social-sign-in-to-a-spring-mvc-web-application-configuration/
    @Override
    public void configure(WebSecurity web) throws Exception {
        web
                //Spring Security ignores request to static resources such as CSS or JS files.
                .ignoring()
                    .antMatchers("/appContextRoot/faces/javax.faces.resource/**");
    }
             
}




Get AppContext from FacesContext;


ApplicationContext ctx = org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

Friday, March 14, 2014

JSF 2.2 HTML5

With JSF 2.2 you can now use html5 syntax and all you need for Jsf to process the element is jsf:id="", html passthroughs are allowed now,  See example below!


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
      xmlns:jsf="http://xmlns.jcp.org/jsf">
    <head jsf:id="head">
        <title>Facelet Title</title>
        <h:outputStylesheet name="css/app.css"/>
    </head>
<body>
<input class="span3" required="required" value="#{index.name}" jsf:id="name" name="name" placeholder="Full Name" type="text"/> 
</body>

</html>

Wednesday, February 6, 2013

JSF CommandLink JQuery Call To BootStrap Modal

JSF h:commandLink with JQuery onclick BootStrap modal.

<h:commandLink onclick="$('#myModal1').modal('show'); return false;" value="Launch Modal"/>


Modal

<br />
<div class="modal hide fade" id="Modal" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal" href="http://www.blogger.com/blogger.g?blogID=5930143319420917182#">×</a>
              <br />
<h3>
Action!</h3>
</div>
<div class="modal-body">
Item Name: #{item.name}<br />
Item Location: #{item.name2}</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" href="#">Cancel</a>
           
              <h:commandlink action="#{bean.method}" styleclass="btn btn-danger btn-small" value="Delete">
            </h:commandlink></div>
</div>

Sunday, November 27, 2011

Display partial Strings using OutputText and JSTL

Here is how you can display a substring in JSF using h:outputText and JSTL.

Add the JSTL functions namespace: xmlns:fn="http://java.sun.com/jsp/jstl/functions" to your JSF page.

Use JSTL in the EL Language: #{fn:substring(item.NameOfItem,0,10)} .

That is it!

Thanks.