Click here to Skip to main content
15,888,351 members
Articles / Web Development / Spring
Tip/Trick

Eliminate the use of view names literal strings in Spring

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
28 Oct 2014CPOL 6.4K   1  
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)


Written By
Software Developer
Egypt Egypt
Enthusiastic programmer/researcher, passionate to learn new technologies, interested in problem solving, data structures, algorithms, AI, machine learning and nlp.

Amateur guitarist/ keyboardist, squash player.

Comments and Discussions

 
-- There are no messages in this forum --