Eclipse创建一个简单的Springmvc程序(Maven工程)

一、建Maven(war)工程
这里写图片描述
报错是没有配置web.xml

  1. 在src->main->webapp 下建WEB-INF文件夹
  2. 在该文件下,建web.xml模板。
    二、配置dom.xml(即导入jar包。)
    在maven仓库中找spring context和spring web mvc,之所以只配置这两个,是因为maven有传递依赖的功能。
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

配置成功后,在Maven Dependencies下会有对应的Jar包
这里写图片描述
三、配置web.xml
在web.xml配置前端控制器:DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <servlet>
    <!-- 默认加载方式
            默认加载必须规范:
                文件命名:servlet-name-servlet.xml===springmvc-servlet.xml
                路劲规范:必须在WEB-INF目录下                          
     -->
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
        <!--Tomcat一启动就运行init()方法-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置拦截器(拦截请求) -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

四、配置mvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--配置springmvc约束。其实和spring约束一样  -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/util 
       http://www.springframework.org/schema/util/spring-util-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/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
    <!--配置注解处理器映射器  
        功能:负责寻找执行类:Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    <!-- 配置注解处理器适配器
        功能:执行Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
    <!--配置视图解析器:InternalResourceViewResolver解析出真正的物理视图
        后返回逻辑视图:index  解析出真正的物理视图:前缀+逻辑视图+后缀====/WEB-INF/pages/index.jsp
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

五、自定义Controller类

package com.nenene;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller //相当于在mvc-servlet.xml配置<bean id="userController" class="UserController路径">
public class UserController {
    @RequestMapping("index")
    public String myHello(){

        return "index";
    }

}

六、定义视图页面
根据视图解析路径:/WEB-INF/pages/
这里写图片描述
注意导入Tomcat,否则创建的jsp文件会报错

七、将项目部署到Tomcat上
右键项目名称->Build Path->Configure Build Path->Libraries->Add Library->Server Runtime->选择继配置好的Tomcat->OK

猜你喜欢

转载自blog.csdn.net/HBlock/article/details/78235322