码迷,mamicode.com
首页 > 编程语言 > 详细

Eclipse使用Maven创建Spring MVC项目(包含实例)以及部署到tomcat服务器

时间:2016-06-12 02:01:42      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:

1、安装Maven并配置环境变量

2、eclipse中配置Maven

3、eclipse创建Maven项目

技术分享
选择项目路径,next

技术分享
选择maven项目类别[maven-archetype-webapp],next

技术分享
输入Group Id(顶级包名),Artifact Id(项目名),Package根据两者自动生成,finish
生成的项目如上图所示。
技术分享
报错原因是缺失Server Runtime Library,添加即可,同时jre替换为System Library。
技术分享
替换后项目目录如下:
技术分享

src文件夹添加resource,创建成功后选中resource目录右键->Build Path->Use as Source Folder;
src/main/webapp/WEB-INF目录下创建lib文件夹以及applicationContext.xml文件
此时项目路径如下
技术分享

编辑pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jiashun</groupId>
  <artifactId>wen</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>wen Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springversion>3.1.1.RELEASE</springversion>
        <junitversion>3.8.1</junitversion>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junitversion}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springversion}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
  <build>
    <finalName>wen</finalName>
  </build>
</project>

选中项目,右键->Run as->Maven install,下载相关maven依赖包(可能需要一定时间)
arc/main/java路径下创建包com.jiashun.controller,com.jiashun.dao,com.jiashun.model,com.jiashun.service,

编辑applicationContext.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        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 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.jiashun.*" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

编辑web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>Archetype Created Web Application</display-name>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>Fuck</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet-mapping>
    <servlet-name>Fuck</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>webAppRootKey</param-name> <!-- 每个web项目的标示 -->
    <param-value>wen.root</param-value>
  </context-param>
</web-app>

src/main/resources目录下创建log4j.properties文件,编辑如下

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=mpsmodel.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n


#log4j.category.org.apache.commons.dbcp=debug
#log4j.category.com.ibatis=debug
#log4j.category.org.springframework=debug

#kai write
log4j.logger.com.ibatis = DEBUG  
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG  
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG  
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG  
log4j.logger.java.sql.Connection = DEBUG  
log4j.logger.java.sql.Statement = DEBUG  
log4j.logger.java.sql.PreparedStatement = DEBUG, fileout  
log4j.logger.java.sql.ResultSet = DEBUG  

项目目录如下:
技术分享

com.jiashun.model包创建Book类

package com.jiashun.model;

public class Book {
    private int id;
    private String name;
    private String author;
    public Book(){}
    public Book(int id, String name, String author) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }


}

com.jiashun.dao包创建BookDao类

package com.jiashun.dao;

import org.springframework.stereotype.Component;

import com.jiashun.model.Book;

@Component
public class BookDao {

    public void add(Book book){
        System.out.print("Add");
    }
    public void update(Book book){
        System.out.print("Update");
    }
}

com.jiashun.service包创建BookService类

package com.jiashun.serivce;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.jiashun.dao.BookDao;
import com.jiashun.model.Book;


@Component
public class BookService {

    private BookDao bookDao;

    public BookDao getBookDao() {
        return bookDao;
    }

    @Resource
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void add(Book book){
        bookDao.add(book);
    }
    public void update(Book book){
        bookDao.update(book);
    }

}

com.jiashun.contorller包创建BookController类

package com.jiashun.controller;

import javax.annotation.Resource;

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

import com.jiashun.model.Book;
import com.jiashun.serivce.BookService;


@Controller
@RequestMapping("/book.do")
public class BookController {

    private BookService bookService;
    @RequestMapping(params = "method=add")
    public String add(Book book){
        System.out.println("bookname:"+book.getName());
        System.out.println("author:"+book.getAuthor());
        bookService.add(book);
        return "success";
    }
    @RequestMapping(params = "method=update")
    public String update(Book book) {
        bookService.update(book);
        return "success";
    }
    public BookService getBookService() {
        return bookService;
    }
    @Resource
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

}

webapp目录下index.jsp编辑

<html>
<body>
<h2>Add Book</h2>
<form method="post" action="<%=request.getContextPath() %>/book.do?method=add">
bookname:<input type="text" name="name" id="name">
author:<input type="text" name="author" id="author">
<input type="submit" value="Add">
</form>
</body>
</html>

webapp目录下创建success.jsp并编辑

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP ‘success.jsp‘ starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    Success! <br>
  </body>
</html>

此时项目目录如下:
技术分享
选中项目,右键->Run as->Run on Server
浏览器URL地址栏输入[http://localhost:8080/wen/index.jsp]或[http://localhost:8080/wen/]得到页面如下:
技术分享
技术分享
点击add跳转到success页面
技术分享

至此,Maven穿件Spring MVC项目并运行完成!

4、项目部署到tomcat中

选中tomcat服务器,删除运行的所有项目并重启
技术分享
选中右键或F3
技术分享
Server Locations选项选择第二个(use tomcat installation…),此时,Server path选项是tomcat的安装目录,比如我的就是(D:\Server\Eclipse\JavaWeb\apache-tomcat-8.0.35)
Deploy path选择webapps,然后保存,这样项目在运行的时候就会发布到tomcat/webapps文件夹下
技术分享
再次启动项目(右键->Run as->Run on Server)即可。
然后就可以直接在tomcat webapps目录下看到项目了。
技术分享
以后就可以直接通过tomcat bin目录下的startup.bat命令启动tomcat服务器,访问[http://localhost:8080/wen/index.jsp]可以得到和上面一样的结果。

Eclipse使用Maven创建Spring MVC项目(包含实例)以及部署到tomcat服务器

标签:

原文地址:http://blog.csdn.net/wenjiashun/article/details/51635688

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!