基于 maven 构件的 spring mvc 框架搭建

来源:互联网 发布:navicat 编写sql语句 编辑:程序博客网 时间:2024/05/29 06:50

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。

  Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具

一、首先是准备工作

1.基本的 jar 包依赖


二、框架搭建

1.创建基本的项目结构


2.在src/man/resources 下创建 spring 配置文件applicationContext.xml 和 sping mvc 配置文件 springmvc-servlet.xml

1) applicationContext.xml 

<?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:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--context:component-scan 会包含此注解<context:annotation-config /> --><!-- 扫描除  @Controller 之外的注解  --><context:component-scan base-package="com.saber.*"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan><!-- 加载属性文件 --><context:property-placeholder ignore-unresolvable="true"location="classpath*:/db.properties" /><!-- 配置数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"p:user="${jdbc.username}"p:password="${jdbc.password}"p:driverClass="${jdbc.driverClass}"p:jdbcUrl="${jdbc.jdbcUrl}"p:initialPoolSize="${jdbc.initialPoolSize}"p:maxPoolSize="${jdbc.maxPoolSize}"/>
2)  springmvc-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><context:component-scan base-package="com.saber.*"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><mvc:annotation-driven/><mvc:default-servlet-handler/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/views/"></property><property name="suffix" value=".jsp"></property></bean></beans>
3.创建web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="false">    <!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>        <!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/saber-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>    <welcome-file-list>  <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    </web-app>
4.创建Controller

@Controller@RequestMapping("/test")public class TestController {@RequestMapping(value="/sayHello", method={RequestMethod.GET, RequestMethod.POST})public ModelAndView sayHello(@RequestParam("name") String name, User user){ModelAndView mav = new ModelAndView();System.out.println("hello: " + user.getName());mav.setViewName("login/hello");return mav;}}
5. 创建页面jsp 略






0 0
原创粉丝点击