views:

182

answers:

3

I have a basic Java EE Spring (MVC) application setup that displays a home page with dynamic content. I am completely new to Spring and am confused how to proceed at this point and add more pages to my application. Do I need to create a new controller for each url on my site? Right now I have the following mapping in my ..-servlet.xml file:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   <property name="mappings">
     <value>
       /index.html=homeController
     </value>
   </property>
</bean>

So if I now have a new page at /login, would I add a mapping to /login/index.html? I get even more confused because I am trying to integrate Spring-security to handle the login page...

A: 

If you are using spring security, you don't need a controller for showing the login form. You can use any jsp page for that purpose and as spring posts it to j_spring_secutity_check, you don't need a controller to handle it too. Check the spring documentation how you can add multiple methods in controller, You may need to use the beanNmaeMapping kind of configuration. Also the better way now is using annotation based config, which helps you configure any pojo as controller with @Controller annotation

Teja Kantamneni
A: 

You could use something like:

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    <property name="mappings">
        <props>
            <prop key="/vehicleSearch">vehicleSearchController</prop>
        </props>
    </property>
</bean>

If there was a file /WEB-INF/jsp/vehicleSearch.jsp it would be mapped to the vehicleSearchController. In this case JSP files are being used for the view but you could adapt it to your view technology.

Configuring it this way you would still need to write a mapping for every file. A better way (as Teja suggested) is probably to annotate the mappings in your controller and do away with the XML configuration.

e.g.

@Controller
@RequestMapping("/vehicleSearch")
public class VehicleSearchController {  
donski
+1  A: 

I would take a look at annotated Controllers:

Example:

@Controller
public class TestController {

    @RequestMapping(value="/login/index.html")
    public String login() {
        return "login";
    }

    @RequestMapping(value="/somethingelse/index.html")
    public String login() {
        return "somethingelse";
    }
}

When you set up your View Resolver, the Strings that are returned would correspond to to a literal page, i.e. somethingelse could be directed to /jsp/somethingelse.jsp if that's how you've set up the resolver in your Spring config. Hint...you need to scan for annotations to auto wire.

Spring-Security is handled in a somewhat similar fashion, but has nothing to do with Spring MVC per say. If done correctly, the only resource you need to provide in order to configure security is the simple login page, which you would configure in your Spring config. Check out this security example:

Droo