国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > 框架设计 > 使用spring 2.5.6搭建mvc框架

使用spring 2.5.6搭建mvc框架

来源:程序员人生   发布时间:2015-01-30 08:54:41 阅读次数:3050次

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>


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生