码迷,mamicode.com
首页 > 编程语言 > 详细

使用spring 2.5.6搭建mvc框架

时间:2015-01-22 09:34:29      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:spring   servlet   controller   requestmapping   

1. Project Dependency


spring 2.5.6所有包: spring 2.5.6 全集下载

commons-logging-1.1.3.jar:  commons-logging-1.1.3下载


2. Controller & Mapping

从2.5开始,spring开始支持@RequestMapping标注,可对符合的url路径进行判别。

HelloWorldController.java:

package controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
	@RequestMapping(method=RequestMethod.GET)
	public String helloWord(ModelMap model){
		System.out.println("hahaha ,nimeiyoushuozhimakaimen");
		model.addAttribute("message","fuck YOU@!!!");
		return "HelloWorldPage";
	}
}

3. Spring Configuration


当该xml被加载之后,能够打开“注解扫描功能”,并定义InternalResourceViewResolver映射规则,将controller中返回的内容映射为前端内容。


mvc-dispatcher-servlet.xml:

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

4. Integrate Web application with Spring

网站服务器server在加载web.xml的时候首先定义欢迎界面(默认:index2.jsp,注意路径的写法),声明ContextLoadListener和DispatcherServlet,以及servlet映射规则。


web.xml:

<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>mySpringwork</display-name>
	<welcome-file-list>
		<welcome-file>/WEB-INF/index2.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

5. JSP Views

HelloWorldPage.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>Message : ${message}</h1>	
 
	<h2>${msg}</h2>
 
</body>
</html>


使用spring 2.5.6搭建mvc框架

标签:spring   servlet   controller   requestmapping   

原文地址:http://blog.csdn.net/langduhualangdu/article/details/42980419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!