搭建springMVC环境并且写一个helloWorld实例

1.new一个maven project。勾上create a simple project,然后点击next
搭建springMVC环境并且写一个helloWorld实例_第1张图片

2.输入groupId 和ArtifactId, Version使用默认就可以,Packaging选war,其他可以不填,然后点击Finish
搭建springMVC环境并且写一个helloWorld实例_第2张图片

3.刚建好的工程,会报错,这是没有关系的,因为我们的web.xml还没有建好
搭建springMVC环境并且写一个helloWorld实例_第3张图片

4.右键工程,选择properties,点击Project Facets里面,去掉Dynamic Web Module勾选,Java选项上版本改为1.7,然后点击apply,再点击ok。
搭建springMVC环境并且写一个helloWorld实例_第4张图片

5.刚才去掉了Dynamic Web Module的勾选,是因为默认的web配置不能满足,我们要重新设置。所以,接下来,再次右键工程,选择properties,点击Project Facets里面,勾上Dynamic Web Module,并把版本改为2.5,点击下面的Further configuration available…,看到如下对话框:
搭建springMVC环境并且写一个helloWorld实例_第5张图片
如下所示:输入:src/main/webapp 并打勾,然后点击ok,在点击applay,点击ok
搭建springMVC环境并且写一个helloWorld实例_第6张图片
此时可以看见我们的目录结构如下,此时我们的工程已经搭建好
搭建springMVC环境并且写一个helloWorld实例_第7张图片

6.在webapp目录下新建一个index.jsp,工程或报错。
搭建springMVC环境并且写一个helloWorld实例_第8张图片
报错之后,我们需要新建一个tomacat服务器,在windows–>preference–>server–>runtime environment
中配置一个apache tomacat 服务器。配置好之后,右键工程,点击Build path,点击configure build path,
点击add libary
搭建springMVC环境并且写一个helloWorld实例_第9张图片
选择server time,然后一直next下
搭建springMVC环境并且写一个helloWorld实例_第10张图片
点击apply,ok。此时我们的工程就不再报错。
搭建springMVC环境并且写一个helloWorld实例_第11张图片

7.打开pom.xml文件,输入以下内容

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.tydic.portalgroupId>
  <artifactId>testSpringartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
    <dependency>  
    <groupId>org.springframeworkgroupId>  
       <artifactId>spring-contextartifactId>  
       <version>4.1.6.RELEASEversion>  
    dependency>  
    <dependency>  
       <groupId>org.springframeworkgroupId>  
       <artifactId>spring-webmvcartifactId>  
       <version>4.1.6.RELEASEversion>  
    dependency>
  dependencies>
 <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                    <compilerArgs>
                        <arg>-parametersarg>
                    compilerArgs>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <configuration>
                    <skipTests>trueskipTests>
                configuration>
            plugin>
        plugins>
    build>  
project>

8.打开web.xml,输入以下内容


<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
<display-name>testSpringdisplay-name>  

       
    <servlet>    
        <servlet-name>spring-mvcservlet-name>    
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>   

          
          <init-param>
        <param-name>contextConfigLocationparam-name>
        
        <param-value>/WEB-INF/spring-mvc-servlet.xmlparam-value> 
        init-param>
        <load-on-startup>1load-on-startup>  
    servlet>    

      
    <servlet-mapping>    
        <servlet-name>spring-mvcservlet-name>       
        <url-pattern>/url-pattern>    
    servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
        <welcome-file>index.htmwelcome-file>
        <welcome-file>index.jspwelcome-file>
        <welcome-file>default.htmlwelcome-file>
        <welcome-file>default.htmwelcome-file>
        <welcome-file>default.jspwelcome-file>
    welcome-file-list>
web-app>

9.在WEB-INF目录下新建一个文件view,在view中新建一个HelloWorld.jsp,内容如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body> 
<h1>message:${message}h1> 
body>
html>

9.在WEB-INF目录下新建一个spring-servlet-mvc.xml,内容如下

 
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="  
        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"> 
       
    <context:annotation-config />
    
    
     <context:component-scan base-package="com.springmvc.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <mvc:annotation-driven/>
    
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        
        <property name="prefix" value="/WEB-INF/view/"/>  
        <property name="suffix" value=".jsp" />  
        <property name="viewClass">  
            <value>org.springframework.web.servlet.view.InternalResourceViewvalue>  
        property>
        <property name="order" value="1"/> 
    bean>  
beans>  

10.在src/main/java 目录下新建一个com.springmvc.controller包,在包里新建一个类Hello,内容如下

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/hello")
public class Hello {

    @RequestMapping(value="/mvc",method=RequestMethod.GET)
    public String HelloWorld(Model model){
        model.addAttribute("message", "Hello world! spring mvc");
        return "HelloWorld";


    }

}

最后项目的目录如下图,项目中有红叉,这是不碍事的。
搭建springMVC环境并且写一个helloWorld实例_第12张图片
11.最后我们右键项目run as–>run on server,显示页面hello world.
搭建springMVC环境并且写一个helloWorld实例_第13张图片

然后我们在地址栏输入:http://localhost:8080/demo/hello/mvc。 见到如下页面,此时我们搭建springMVC环境并且写一个helloworld实例结束
搭建springMVC环境并且写一个helloWorld实例_第14张图片

你可能感兴趣的:(spring)