Java初体验之springMvc(一) springmvc框架的的引入

来源:互联网 发布:机械 simulink 知乎 编辑:程序博客网 时间:2024/06/02 06:22

第一章,项目搭建以及springMvc的配置

Java这门语言我心驰神往已久,就让我们来一起做一个web项目,预计用到的东西有如下.

  • js
  • springMvc
  • json
  • sql
  • 定时任务

第一步 创建项目

我这里用的是eclipse,当然你们如果用其他的IDE也没有什么问题大同小异。其他的IDE我推荐是IDEA,这款用java的swing技术开发的一款IDE在操作的流畅程度来说还是不错的,但是对配置要求比较高
这里写图片描述

第二步 编写web.xml

这一步比较重要,可以说是特别重要。web.xml的位置在WebContent下面的web-inf下面。

<?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" 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>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>        login.jsp        </welcome-file>    </welcome-file-list></web-app>

第三步 编写springmvc-servlet.xml

重要的是springmvc-servlet.xml和web.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:mvc="http://www.springframework.org/schema/mvc"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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <!-- mvc的注解驱动 -->       <context:annotation-config/>    <!-- 扫描器 -->    <context:component-scan base-package="com.*"></context:component-scan>    <!-- 前缀+viewName+后缀 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <!--webroot到某一指定文件夹的路径-->    <property name="prefix" value="/WEB-INF/jsp/"></property>    <!--视图名称的后缀-->    <property name="suffix" value=".jsp"></property>    </bean></beans>

第四步 添加到tomcat中

这里写图片描述

然后启动tomcat,启动没有报错,那么我们的项目基本的结构就已经准备好了

下面是springMvc使用到的jar包 http://pan.baidu.com/s/1cJb5lc 密码v4jb