基于 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
- 基于 maven 构件的 spring mvc 框架搭建
- 基于maven的spring mvc框架引入
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
- spring起步 搭建基于Maven的Spring MVC工程
- 基于 maven 的 ssm 项目搭建三:整合 spring mvc
- IDEA 创建基于Maven的spring mvc框架
- IDEA 创建基于Maven的spring mvc框架
- spring MVC 框架的搭建(myeclipse +maven)--(上)
- spring MVC 框架的搭建(myeclipse +maven)--(下)
- Maven项目搭建Spring MVC框架
- idea 搭建maven+spring mvc+jpa框架
- 基于注解的Spring MVC 框架搭建及配置详解
- 基于Maven的SSM框架的搭建(Spring+SpringMVC+MyBatis)
- 基于maven的springmvc +hibernate+ spring框架搭建
- 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建
- 基于maven的spring+mybatis+springMVC框架搭建
- 基于Spring MVC框架搭建项目
- log4j
- DNS 域名系统
- A*搜索算法
- 面试大总结之二:Java搞定面试中的二叉树题目
- 什么是滚动优化(receding-horizon/rolling-horizon)?
- 基于 maven 构件的 spring mvc 框架搭建
- linux shell下的输入输出重定向
- 多线程练习题
- android onTouch()与onTouchEvent()的区别
- 关于citynight.cn&mekor.cn空间忘了续费导致所有资料丢失
- 看到别人用css3写的baymax ,大白,挺萌的
- python 简单图像处理(8) 直方图均衡化
- 关于Android的SDK更新下载过程说明
- 我的企业消费黑名单