lundi 27 juin 2016

How to use jsf with spring boot

I have created a demo spring boot application. It is working fine. Now I want to integrate jsf. A little heads up would be nice on how to integrate with spring boot. Although I found some articles related how to integrate with spring. And 2-3 articles on how to integrate with spring boot. But I am very confused because each article used different approach.

Below is my simple demo app code.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testapp</groupId>
    <artifactId>Test-App</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Test-App</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

IndexController

package com.testapp.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

}

Index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<title>Demo app</title>
</head>
<body>
<div>
    <form>
        First name:<br />
        <input type="text" name="firstname" /><br />
        Last name:<br />
        <input type="text" name="lastname" />
    </form>
</div>
</body>
</html>

I also have a user model with firstname and lastname as property. So what I need here is to integrate jsf so that the above form could be replaced by a jsf form and then submit the form to userlogin() action of IndexController and get the get the parameters on variable.

So IndexController will have a action like below

public String userlogin(){
String firstname = firstname of the form input
String lastname = lastname of the form input
return "home"
}

Maybe the answer will be little longer for this answer. But I think a clean integration of spring boot and jsf with a form handling will be helpful for beginners.

Aucun commentaire:

Enregistrer un commentaire