Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Spring

Eliminate the use of view names literal strings in Spring

4.00/5 (1 vote)
28 Oct 2014CPOL 6.6K  
Avoid typing view names in Spring controllers by hand

Bitbucket repo

 

T4Spring is a code generation tool for Spring MVC apps that creates strongly typed views that eliminate the use of literal strings in many places. e.g. instead of:

Java
public String index(ModelMap map)
{
    map.addAttribute("msg", "literal strings are no good!!");
    return "Home/index";
}


T4Spring lets you write:

Java
public String index(ModelMap map)
{
    map.addAttribute("msg", "Hello T4Spring!!");
    return Views.Home.index;
}


How to use:

  1. Get plugin from Maven
  2. Add this to your pom
  3. Build your project
XML
<dependency>
        <groupId>omargameelsalem</groupId>
        <artifactId>T4Spring</artifactId>
        <version>1.0</version>
</dependency>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>  
                    org.omargameelsalem.t4spring.Main.generate(System.getProperty("user.dir"));
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)