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
- Spring MVC笔记
- spring mvc 基础笔记
- spring mvc 笔记
- Spring MVC 学习笔记
- Spring MVC笔记
- Spring MVC 笔记1
- Spring MVC笔记
- Spring MVC笔记
- spring MVC学习笔记
- Spring Mvc 笔记
- 孔浩Spring mvc 笔记
- spring mvc笔记
- Spring MVC学习笔记
- spring-mvc学习笔记
- spring mvc学习笔记
- spring mvc 学习笔记
- Spring MVC 学习笔记
- Spring MVC笔记
- AJAX异步请求——练习
- hdu 1829 A Bug's Life(并查集+偏移向量)
- U盘安装Kali 2.0 cd-rom无法挂载解决方法
- 如何给App快速搭建虚拟服务器
- BC 62 (div.2) B
- Spring MVC笔记
- tokudb学习
- Arduino与Android蓝牙通信笔记
- APP中使用UI交互设计动效的三个好处
- Button设置单框不显示
- 启动控制台程序,重定向输入输出流
- muduo的reactor模式基本实现
- easyui datagrid 编辑状态时,为列添加文本改变事件
- 欢迎使用CSDN-markdown编辑器