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

一、建Maven(war)工程
Eclipse创建一个简单的Springmvc程序(Maven工程)_第1张图片
报错是没有配置web.xml

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

配置成功后,在Maven Dependencies下会有对应的Jar包
Eclipse创建一个简单的Springmvc程序(Maven工程)_第2张图片
三、配置web.xml
在web.xml配置前端控制器:DispatcherServlet


<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>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet servlet-class>
        
        <load-on-startup>1load-on-startup>
    servlet>
    
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
web-app>

四、配置mvc-servlet.xml



<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
">
    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean>
    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean>
    
    <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配置
public class UserController {
    @RequestMapping("index")
    public String myHello(){

        return "index";
    }

}

六、定义视图页面
根据视图解析路径:/WEB-INF/pages/
Eclipse创建一个简单的Springmvc程序(Maven工程)_第3张图片
注意导入Tomcat,否则创建的jsp文件会报错

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

你可能感兴趣的:(JAVA,maven)