springMVC项目的搭建

一、新建一个Maven项目

File->New-> maven project
这里写图片描述
单击next,选择Maven Webapp project. 单击next
这里写图片描述
填写好groupId 与artifactId这里写图片描述
单击finish 即可得到项目 目录结构如图所示。
这里写图片描述
红叉问题:Description Resource Path Location Type The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path index.jsp /springMVC-demo/src/main/webapp line 1 JSP Problem。
出现错误的原因:未设置server runtime environment
解决的办法:右击项目->build path->configure build path…->add library ->server runtime
这里写图片描述
根据自己需要设置对应的server runtime 环境。

添加src/main/java,和src/text/java,项目右键Build path—-Configure Build Path,双击include(all),点击add–填写项目名称/src/mian/java—-Finsh—-OK,即可添加上所缺少的文件。
这里写图片描述

二、在maven的pom.xml文件中导入需要的项目依赖

在pom.xml中插入如下代码。

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

三、配置spring-context.xml文件

在src/main/resources目录下新建一个xml文件spring-context.xml。添加如下配置

<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-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">


    <!-- 扫描web相关的bean -->
     <context:component-scan base-package="com.zsh.controller"/>
    <mvc:annotation-driven></mvc:annotation-driven>


</beans>

四、配置web.xml文件

在web.xml中添加如下配置,让servlet容器加载spring的上下文和配置spring的dispatchServlet。

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-context.xml</param-value>
  </context-param>

  <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

六、编写controller控制器

在src/main/java目录下建一个包com.zsh.controller在改包中新建一个类HelloController

@Controller
@RequestMapping("/")
public class HelloController {
     @RequestMapping(value="hello",produces="text/html; charset=UTF-8")
        public @ResponseBody String homePage(){
            return "第一个spingMVC项目搭建成功";
        }
}

七、启动项目

启动项目在浏览器中输入http://localhost:8080/springmvc-web-demo/hello即可得到输出结果。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhangsuhua0702/article/details/81333113