Tuesday, May 24, 2011

Advanced Data Model with Rails

Model, View, Controller. If you’ve ever tried Ruby on Rails, those words have probably been drilled into your head a thousand times over. On the other hand, if this is new to you, there’s plenty of resources on Nettuts+ for beginners – but this is not one of them. ...

Read more...

Wednesday, April 22, 2009

Simplify Your Spring IoC Containers Using Shortcuts

Spring 2.X only. If you have a large amount of lines in your IoC container, you can minimize them by using shortcuts. You can accomplish this by using the p schema.
p="http://www.springframework.org/schema/p

...
...
...

Let's say you have this simple bean configuration:

   
    ABC 
  

Using the schema, you can simply define your properties like this:

If you need to reference a property to another bean or class, simply add -ref.

   
    varBean
  


Tuesday, March 3, 2009

Tutorial: Your First Simple Spring MVC Template

In this post, I'm going to show you how to create your first simple Spring MVC template. Before we start, I assume that you already know the basics of Spring MVC. If you don't, I suggest to read first some introductions to Spring. Below are some good resources to start. Let's start. Setup a new blank project. Set all API dependencies in your classpath then configure your web.xml like this:




myproject
org.springframework.web.servlet.DispatcherServlet
1



myproject
/index.html



myproject
*.html



index.html



I set two mappings, one for the url pattern and the other one is for the welcome page. Next is setting-up the beans. In your myproject-servlet.xml, put these configurations:



 





 showIndex






 
    myProjectController







I used the ResourceBundleViewResolver to configure the views using properties file in order for us to still edit the configurations of views without recompiling the project. The bean actionMethodNameResolver is where all your url requests will be dispatched to its proper controller. Since the prop /index.html has a value of showIndex, Your PageController should contain this method:
package com.myproject.controllers;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageController extends MultiActionController {

public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(index-page, "model", "Hello World!");
}
}
Next step is to create the view index-page. Create a properties file named views. It should contain the class and the url of your jsp. Take note that you need the jstl lib in your classpath.
index-page.class=org.springframework.web.servlet.view.JstlView
index-page.url=WEB-INF/index.jsp
Create the index.jsp under WEB-INF. To get the model from our controller, we can use this code.

${model}

Set up your Tomcat server. Deploy and run the project. Access your local server (http://localhost:[port]) and your done. You should see the "Hello World!". You can now add other page requests on your actionMethodNameResolver's props and configure the proper method in your PageController. Don't forget to add your views configurations in the views.properties. You can download the source code here. I created the project using Idea IntelliJ and included the required libraries so you don't have to download them. You can also import the project using Apache Ant. If you have questions, post in comments.