maven构建springmvc项目

 

 

1.用maven插件构建项目框架

 

 

 

2.完善项目

  首先,完善目录,增加重要的source Folder,这个不是简单的Floder,这些文件夹是会参与编译的。增加src/main/java,src/test/resources,src/test/java目录。让目录变成标准的maven结构。如下图:

 

 

接下来,改变一些配置:

让工程的JDK用本地的jdk;

让工程的字符集为UTF-8;

改变工程的目录顺序;

 

3.修改web.xml(此时需注意,如果之前修改了Dynamic Web Module的版本,则需要修改web.xml头部为对应的版本),添加Servlet(此处使用的是3.0版本的web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

如上配置会自动去WEB-INF下寻找'servlet-name'-servlet.xml(此处对应为spring-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.springdemo.*" />

    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
    <mvc:annotation-driven />

    <!-- 访问静态资源 -->
    <mvc:default-servlet-handler />
    
    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

此配置会自动扫描com.springdemo下的所有包中的含有注解的类(如@Controller, @Service等);<mvc:annotation-driven />会注册两个映射类,负责将请求映射到类和方法中;因为配置的spring是拦截所有请求,所以需要配置<mvc:default-servlet-handler />,来让静态资源通过(如js, css文件等);视图解析器是将Controller类返回的视图名加上配置的前后缀进行展示。

  在WEB-INF下创建view文件夹,并在其中创建jsp页面:

 

<%@ 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>首页</title>
</head>
<body>
    <h1>This is SpringMVC Demo</h1>
</body>
</html>
package com.springdemo.controller;

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

@Controller
public class DemoController {

    @RequestMapping("/index")
    public String index(){
        return "demo";
    }
}

 

posted on 2017-07-16 18:08  code-java  阅读(188)  评论(0)    收藏  举报

导航