程序员人生 网站导航

使用spring 2.5.6搭建mvc框架

栏目:框架设计时间:2015-01-30 08:54:41

1. Project Dependency


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

commons-logging⑴.1.3.jar:  commons-logging⑴.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⑵.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑵.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⑻859⑴" pageEncoding="ISO⑻859⑴"%> <!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⑻859⑴"> <title>Insert title here</title> </head> <body> <h1>Message : ${message}</h1> <h2>${msg}</h2> </body> </html>


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐