Understanding files created by the Spring MVC project template

Let's examine some of the files created by the template:

  • src/main/webapp/WEB-INF/web.xml: A front Controller servlet is declared here, along with other configurations:
<!-- Processes application requests --> 
<servlet> 
  <servlet-name>appServlet</servlet-name> 
  <servlet- 
class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-
context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

DispatcherServlet is the front Controller servlet. It is passed the path of the context (XML) file for configuring Spring DI. Recall that in the standalone Spring application, we created context.xml to configure dependency injection. The DispatcherServlet servlet is mapped to handle requests to this web application.

  • src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml: Context configuration for Spring DI. Some of the notable configuration parameters in this file are as follows:
<annotation-driven /> 

This enables annotations for configuring dependency injection at the class level:

<resources mapping="/resources/**" location="/resources/" /> 

Static files, such as CSS, JavaScript, and images, can be placed in the resources folder (src/main/webapp/resources):

<beans:bean 
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>

This tells Spring to use the InternalResourceViewResolver class to resolve Views. Properties of this bean tell the InternalResourceViewResolver class to look for the View files in the /WEB-INF/views folder. Furthermore, Views will be JSP files, as indicated by the suffix property. Our Views will be the JSP files in the src/main/webapp/WEB-INF/views folder:

<context:component-scan base-package="packt.jee.course_management" /> 

This tells Spring to scan the packt.jee.course_management package and its sub-packages to search for components (annotated by @Component).

The default template also creates one Controller and one View. The controller class is HomeController in the package that you specified in the Spring project wizard (in our example, it is packt.jee.course_management). Controller in Spring MVC is called by the dispatcher servlet. Controllers are annotated by @Controller. To map the request path to a Controller, you use the @RequestMapping annotation. Let's see the code generated by the template in the HomeController class:

@Controller 
public class HomeController { 
 
  private static final Logger logger = 
LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); Date date = new Date(); DateFormat dateFormat =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate ); return "home"; } }

The home method is annotated with @RequestMapping. The value of mapping is /, which tells the dispatcher servlet to send all requests coming its way to this method. The method attribute tells the dispatcher to call the home method only for HTTP requests of the GET type. The home method takes two arguments, namely Locale and Model; both are injected at runtime by Spring. The @RequestMapping annotation also tells Spring to insert any dependencies when calling the home method, and so locale and model are auto-injected.

The method itself does not do much; it gets the current date-time and sets it as an attribute in the Model. Any attributes set in the Model are available to the View (JSP). The method returns a string, "home". This value is used by Spring MVC to resolve the View to be displayed. The InternalResourceViewResolver that we saw in servlet-context.xml previously resolves this as home.jsp in the /WEB-INF/views folder. home.jsp has the following code in the <body> tag:

<P>  The time on the server is ${serverTime}. </P> 

The serverTime variable comes from the Model object set in the home method of HomeController.

To run this project, we need to configure a server in Eclipse and add this project to the server. Refer to the Configuring Tomcat in Eclipse and Running JSP in Tomcat sections in Chapter 2, Creating a Simple JEE Web Application.

Once you configure Tomcat and add the project to it, start the server. Then, right-click on the project and select Run As | Run on Server. You should see a hello message with the timestamp displayed in the internal Eclipse browser. The URL in the browser's address bar should be http://localhost:8080/course_management/, assuming that Tomcat is deployed on port 8080 and the context name (derived from the top-level package name) is course_management. If you want to change the default context name or remove the context, that is, deploy the application in the root context, then open the project properties (right-click on the project and select Properties) and go to Web Project Settings. You can change the context root name or remove it from this page:

Figure 8.4: Context root setting

For our Course Management application, we are not going to need the HomeController class or home.jsp, so you can go ahead and delete these files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.42.176