首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

Spring MVC(一)

2013-04-05 
Spring MVC(1)Spring不仅可以集成第三方web框架,同时自己也提供了一套web框架作为使用,今天就先来看下如何

Spring MVC(1)

Spring不仅可以集成第三方web框架,同时自己也提供了一套web框架作为使用,今天就先来看下如何搭建一个简单的基于spring MVC的wenb项目

1. 准备jar

? ? 可以直接将spring下载的jar都丢入进去,具体可以查看截图信息

? ?
Spring MVC(一)
? ?项目结构:

? ?
Spring MVC(一)
?2.配置springConfig.xml文件

? ?我将该文件存放在WEB-INF/config目录下面,该目录主要用来存储配置文件信息

??

<?xml version="1.0" encoding="UTF-8"?><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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 将请求的url和bean映射起来,如访问springMVC/login,那么spring配置文件中需要有个login的bean来与之对应 --><bean value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean><bean name="/test.do" name="code"><?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><description>spring-servlet.xml</description><param-name>contextConfigLocation</param-name><param-value>WEB-INF/config/springConfig.xml</param-value></init-param><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>

? ? spring是通过org.springframework.web.servlet.DispatcherServlet来进行分发客户端请求的,因此我们需要拦截所以的.do的访问,然后根据bean的配置进行分发处理

? ?这里如果不加contextConfigLocation这个参数,那么系统会默认到WEB-INF下面查找spring-servlet.xml

即[servlet-name]-servlet.xml

?

4.编写TestController

? ?我们的controller需要实现org.springframework.web.servlet.mvc.Controller,并且在handleRequest方法中进行数据处理。

package com.jacksoft.spring.mvc.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class TestController implements Controller {public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {ModelAndView mv = new ModelAndView();mv.setViewName("test");mv.addObject("msg", "SUCCESS!!");System.out.println("-------------TestController处理请求---------------------");return mv;}}

? ?其中mv.setViewName是设置视图名称,也就是返回的jsp页面

?

5. 编写test.jsp

? ? ?由于前面配置前缀是/WEB-INF/JSP/ ,所以我们的jsp应该写到该目录下面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!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=utf-8"><title>Insert title here</title></head><body>Hello ,Spring MVC<br>${msg}</body></html>

?

6.访问

? 地址栏输入http://localhost:8080/springMVC/test.do,能进行正常访问,页面输出:

Hello ,Spring MVCSUCCESS!!

? ?表示配置成功!

?

注意:

当我们在配置DispatcherServlet分发时,对url-pattern最好不要直接写成这样:

<servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>

?因为这样是拦截所有,当controller返回视图时,spring又把返回的路径作为请求进行拦截,所以就可能会出现下面的错误:

警告: No mapping found for HTTP request with URI [/springMVC/WEB-INF/jsp/test.jsp] in DispatcherServlet with name 'spring'

?

热点排行