Spring MVC笔记

来源:互联网 发布:mysql批量更新数据 编辑:程序博客网 时间:2024/06/03 20:59

Required Configuration

You need to map requests that you want the DispatcherServletto handle, by using a URL mapping in the web.xml file. The following is an example to show declaration and mapping forHelloWeb DispatcherServlet example:

<web-app id="WebApp_ID" version="2.4"    xmlns="http://java.sun.com/xml/ns/j2ee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     <display-name>Spring MVC Application</display-name>   <servlet>      <servlet-name>HelloWeb</servlet-name>      <servlet-class>         org.springframework.web.servlet.DispatcherServlet      </servlet-class>      <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>      <servlet-name>HelloWeb</servlet-name>      <url-pattern>*.jsp</url-pattern>   </servlet-mapping></web-app>
If you do not want to go with default filename as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF, you can customize this file name and location by adding the servlet listener ContextLoaderListener in your web.xml file as follows:
<web-app...><!-------- DispatcherServlet definition goes here----->....<context-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value></context-param><listener>   <listener-class>      org.springframework.web.context.ContextLoaderListener   </listener-class></listener></web-app>

<beans xmlns="http://www.springframework.org/schema/beans"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="   http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd">   <context:component-scan base-package="com.tutorialspoint" />   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <property name="prefix" value="/WEB-INF/jsp/" />      <property name="suffix" value=".jsp" />   </bean></beans>

Defining a Controller

@Controllerpublic class HelloController{    @RequestMapping(value = "/hello", method = RequestMethod.GET)   public String printHello(ModelMap model) {      model.addAttribute("message", "Hello Spring MVC Framework!");      return "hello";   }}

@RequestMapping(method = RequestMethod.GET) is used to declare the printHello()method as the controller's default service method to handle HTTP GET request. You can define another method to handle any POST request at the same URL.

Creating JSP Views

<html>   <head>   <title>Hello Spring MVC</title>   </head>   <body>   <h2>${message}</h2>   </body></html>
Here ${message} is the attribute which we have setup inside the Controller. You can have multiple attributes to be displayed inside your view.

0 0
原创粉丝点击