go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  MVC -- Controller
 
Subject: MVC -- Controller
Author: WebSpider
In response to: Spring MVC -- Configuration
Posted on: 11/23/2017 02:35:24 AM

@Controller
public class HomeController {
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String home(Locale locale, Model model) 
        {
		String server_timestamp = new Date().toString();
		model.addAttribute("serverTime", server_timestamp);
		return "home";
	}
}


With the above code, the Spring Framework can do following jobs:
  • @Controller -- The class HomeController has been annotated as a controller
  • @RequestMapping(value = "/index", method = RequestMethod.GET) -- The HTTP GET request http://<host>:<port>/<context>/index will be served by the annotated method home(Locale locale, Model model)
  • Locale locale, Model model -- Dependencies will be injected silently under the hood by Spring Framework
  • return "home" -- The view will be rendered by "<prefix>home<suffix>" with the help of dispatch-servlet.xml


     

    > On 11/23/2017 02:31:39 AM WebSpider wrote:


    /WEB-INF/web.xml
    	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/spring/*.xml</param-value>
    	</context-param>
    	
    	<!-- LISTENERS -->
    	<!-- Creates the Spring Container shared by all Servlets and Filters -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    
    	<!-- FILTERS -->
    	<!-- Creates the filters shared by all Servlets and Filters -->
    	<!-- ... -->
    
    	<!-- SERVLETS -->
    	<servlet>
    		<servlet-name>myServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/myServlet/dispatch-servlet.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    		
    	<servlet-mapping>
    		<servlet-name>myServlet</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    



    dispatch-servlet.xml
    	<!-- Resolves views selected for rendering by @Controller  -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<!-- bean injection by constructor 
    		<constructor-arg></constructor-arg>
    		-->
    		<!-- bean injection by setter -->
    		<property name="prefix" value="/WEB-INF/views/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<!-- Auto-detection for annotation @Controllers  -->
    	<context:component-scan base-package="com.xyz.springmvc" />
    	<context:component-scan base-package="com.xyz.springmvc2" />
    
    





    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.