Spring Mvc那点事---(3)Spring Mvc项目创建

来源:互联网 发布:斯隆女士原型知乎 编辑:程序博客网 时间:2024/05/16 07:52

        我们这节看下怎么创建Spring MVC项目,通过MAVEN来创建项目,Spring MVC项目的创建需要引用第三方的组件,包括Spring-MVC,配置Tomact服务器等。

      1.Spring MVC项目创建

        打开esclipse,选择菜单 File-->New--Project,选择maven,创建项目,如下图

       

        点击下一步,选择创建目录

       

       点击下一步

      

       点击下一步,输入项目名称,GroupId表示项目的组织名称,Artifact Id表示项目名称,一个GroupId下面可以有多个项目

      

        点击完成后,项目创建成功,目录结构如下图

      

       默认情况下 src/main/java目录结构可能不存在,可以通过New-->Source Folder创建,

       如果创建不成功,右键选中项目--属性,找到java-bulid-path面板,选择Source选项卡,如果选项卡中已经存在,并且显示红色叉号,可以先删除,然后就可以创建了。

       如下图:

         

        2.项目文件配置

          POM.XML文件

          POM.XML文件是MAVEN的项目管理文件,通过在文件中配置可以自动引用第三方jar包。我们创建Spring MVC,需要引用spring相关组件,我们可以添加如下 spring组件配置信息。如下:

       

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.springmvc</groupId>  <artifactId>springmvcfirst</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>springmvcfirst Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies><!-- http servlet配置 -->  <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency>  <!-- spring mvc web组件配置 -->  <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.5.RELEASE</version></dependency>  <!-- spring 组件配置 -->  <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.5.RELEASE</version></dependency>        <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>  <build>    <finalName>springmvcfirst</finalName>  </build></project>

            关于组件的配置信息,可以在 http://mvnrepository.com/这个地方搜索


 web.xml文件,web.xml是整个srping.mvc网站的全局配置文件,包括springmvc的配置,spring的具体配置,

       

<?xml version="1.0" encoding="UTF-8"?>  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"      version="3.1">        <display-name>Spring MvC First</display-name>  <description>Spring MvC First</description>     <!-- Spring配置-->         <context-param>             <param-name>contextConfigLocation</param-name>             <param-value>/WEB-INF/applicationContext.xml</param-value>         </context-param>             <!-- 字符集 过滤器  -->      <filter>          <filter-name>CharacterEncodingFilter</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <init-param>              <param-name>encoding</param-name>              <param-value>UTF-8</param-value>          </init-param>          <init-param>              <param-name>forceEncoding</param-name>              <param-value>true</param-value>          </init-param>      </filter>      <filter-mapping>          <filter-name>CharacterEncodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>            <!-- Spring监听 -->         <listener>             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>         </listener>          <!-- Spring view分发器 -->      <servlet>          <servlet-name>dispatcher</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>          </init-param>          <load-on-startup>1</load-on-startup>      </servlet>      <!-- 请求后缀 -->    <servlet-mapping>          <servlet-name>dispatcher</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping>  </web-app>


   dispatcher-servlet.xml文件是专门针对spring mvc的配置,里面有对页面路径,后缀访问的配置,改文件的名称是有<servlet-name>dispatcher</servlet-name>  节点的值加上-servlet.xml组成.

 

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"          xmlns:aop="http://www.springframework.org/schema/aop"          xmlns:context="http://www.springframework.org/schema/context"         xmlns:mvc="http://www.springframework.org/schema/mvc"          xmlns:tx="http://www.springframework.org/schema/tx"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-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/mvc           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd           http://www.springframework.org/schema/tx           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        <mvc:annotation-driven />      <context:component-scan base-package="com.springfirst.Controller" />      <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/views/" />          <property name="suffix" value=".jsp" />      </bean>    </beans>  

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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "    default-lazy-init="true">    <description>Spring配置</description>  </beans>


      这几个配置文件是spirng mvc的核心配置文件。以后我们需要增加更多配置可以在这几个文件里面添加。
       

    3.添加控制器和页面

       控制器controller是对用户处理请求的,控制器接受用户的输入并调用模型和视图去完成用户的需求,我们开看看怎样添加控制器
       右键选中src/main/java,  new-->Class,如下图
       
        代码如下
       
package com.springfirst.Controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/Home")public class HomeController {@RequestMapping(value="index")public String Index(){System.out.print("123");return "index";}}

      添加页面,在WEB-INF文件夹下创建views文件夹,添加ndex.jsp页面,,右键选中文件夹  new-->jsp..flle
<%@ 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 World,我的第一个spring mvc项目</body></html>

   4.运行项目

          项目创建完成后,我们要看到预览效果,这里我们使用tomcat服务器运行页面。首先去tomcat官网下载tomcat服务器,然后我们按如下步骤添加。
          找到菜单windows-->server->run environment,点击add添加
          
                 

               添加完成后,在Servers,中添加tomcat服务器.如下图
                 

   启动服务器,右键选中服务器,点击Start, 启动,  然后在浏览器中输入地址就可以
  http://localhost:8092/springmvcfirst/Home/index
   
   项目结构图
   

代码下载  http://download.csdn.net/detail/zx13525079024/9513659



0 0
原创粉丝点击