使用Maven、Eclipse搭建SpringMVC

Eclipse使用maven,一步步搭建spring mvc,供你们参考,具体内容以下html

一、 环境配置java

使用maven版本:apache-maven-3.3.3web

使用tomcat版本:apache-tomcat-7.0.64spring

使用jdk版本:jdk1.7.0_67apache

spring-webmvc: jar包版本 4.3.3.Releaseapi

二、建立maven工程spring-mvc

a). 打开eclipse,file->new->project->Maven->Maven Projecttomcat

b). 下一步服务器

201605250844225.png (650×586)

c). 选择建立的工程为webapp,下一步mvc

201605250844226.png (650×586)

d). 填写项目的group id和artifact id。通常状况下,group id写域名的倒序,artifact id写项目名称便可。最后点完成。

生成后的项目会报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。经过提示信息可知是因为找不到HttpServlet类,可经过导入Tomcat到工做目录或者经过Maven添加HttpServlet类所在的servlet-api.jar。

这里经过Maven添加HttpServlet类所在的servlet-api.jar。

 

这里提下:点击pom.xml中的Add按钮,在中间输入框中分别输入servlet-api时,出现IDE中并不会出现正常的select界面

缘由:应该是maven自动下载index组件的功能没有开启,查看

"Window" --> "Preferences" and choose Maven in the left side.勾选Download repository index updates on startup

解决:

"Window" ---> "Show View" ---> "Maven Repositories" ---> "Global Repositories" 

选择 "central" click right and "Update Index"解决问题。

e).如今经过maven添加SpringMVC所需jar包,点击pom.xml中的Add按钮,在中间输入框中分别输入spring-webmvc:

若是知道依赖的group id和artifact id,能够直接填写,若是不清楚,能够输入关键字进行查询,或是到http://search.maven.org网站查询

保存下 ,看看对应的pom文件有没有问题,此时项目也不出错了。

f).  打开src/main/webapp/WEB-INF/web.xml文件,最终修改内容以下:

<web-app 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"
      version="3.0">
      <!-- Spring MVC配置 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
   <!-- spring mvc 请求后缀 -->
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
g)在src – main目录下新建文件夹Java

首先移除missing包文件

而后点击Add Folder后 ->选中mian后->点击Create New Folder 

最后生成

h)、在java中新建类HelloController.java。包名为com.springmvc.controller。

i)、编写HelloController类里代码以下:

package com.springmvc.controller;

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

@Controller
public class HelloController {
    @RequestMapping(value="/Hello")
    public String HelloWorld(Model model){
        model.addAttribute("message","Hello World!!!SpringMVC");
        return "Helloworld";
    }
    
}
 

j)、在WEB-INF下 新建文件夹view 在新建Helloworld.jsp.

k)、Helloworld.jsp代码以下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>message:${message}</h1>
</body>
</html>

l)、在src – main –webapp – WEB-INF目录下新建文件spring-mvc-servlet.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:component-scan base-package="com.springmvc.controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

m)、接下来能够发布到tomcat服务器下,启动项目访问路径http://localhost:8080/springmvc-demo/Hello 界面以下

具体使用可见官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/

相关文章
相关标签/搜索