eclipse基于Maven的springMVC框架搭建

eclipse基于Maven的springMVC框架搭建

本篇文章详细介绍了maven的SpringMVC项目搭建,入门级别的教程,适用于新手。按照流程中出现任何问题,请拉到最后看构建过程中遇到的坑,是否能解决问题,解决不了的话就只能百度了。

搭建maven项目

  • 1、新建工作空间,名字随便了,这里我用的workspacetest
  • 2、配置eclipse的jre,Windows->Properties->Java->Installed,因为是开发环境,所以一定要配置成jdk
  • 3、设置自己的maven,properties->Maven,主要是设置.settings和本地maven库的路径
  • 4、创建maven项目file->new->mavenProject->next,注意选择webapp
    maven建立项目选项
  • 5、建立项目,其中Group Id是公司的网址倒写,例如net.csdn,Artifact Id是项目名称,Package为包路径,下一步需要重新规划,此处不处理。
    在这里插入图片描述
  • 6、重新如下图路径
    在这里插入图片描述
  • 7、添加maven支持,就是处理pom.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.air</groupId>
  <artifactId>springModules</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springModules Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <!-- spring版本号 -->
  <properties>
      <spring.version>4.1.1.RELEASE</spring.version>
  </properties>
  
<!-- <profiles>
	<profile>
		<id>jdk-1.8</id>
		<activation>
			<activeByDefault>true</activeByDefault>
			<jdk>1.8</jdk>
		</activation>
		<properties>
			<maven.compiler.source>1.8</maven.compiler.source>
			<maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		</properties>
	</profile>
</profiles>-->
  <!-- 以下是依赖设置 -->
  <dependencies>
  	<!-- 添加单元测试依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>provided</scope>
    </dependency>
    
    <!-- 添加servlet依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
        <version>3.1.0</version>
    </dependency>
    
    <!-- 添加spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>springModules</finalName>
    <plugins>
    	<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <path>/springModules</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <server>tomcat8</server>
                </configuration>
            </plugin>
            
            <plugin>
            	<groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
    </plugins>
  </build>
</project>
  • 8、增加srpingMVC配置文件,springmvcContext.xml,路径放在src->main->resource下面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    
    <!-- 搜索spring控件 -->
    <context:component-scan base-package="com.test"></context:component-scan>
    <!-- 视图页面配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>
  • 8、配置web.xml,注意webxml中的路径和文件名要和实际的对应,这里有个点就是contextConfiLocation的路径设置,这个就自行百度吧。
<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">
    
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
             <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvcContext.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvcContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>
  • 9、编写demo,放在com.test路径下面,编写index.jsp,放在WEB-INFO->views下面
package com.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
@RequestMapping("/demo")
public class Demo{

	@RequestMapping("/salary")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv=new ModelAndView("index");//指定视图
        double basicSalary = 7000;		//基础工资
        double extWorkSalary = 1200+600;//加班费
        double otherSubside = 400;		//额外补贴
        double medicalExtSubide = -5.5;	//大额医疗
        
        double pensionRatio = 0.08;//养老缴费比例
        double injuryRatio = 0.005;//工伤缴费比例
        double medicalRatio = 0.02;//医疗缴费比例
        double publicFund = 0.08;//公积金缴费比例
        
        double salaryRemain = basicSalary + extWorkSalary + otherSubside + medicalExtSubide
        				-basicSalary*(pensionRatio+injuryRatio+medicalRatio+publicFund);
        
        mv.addObject("mvstr", "Hello World!"+"The Money you can get :"+salaryRemain);
        
        return mv;
    }
}
<%@ 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>Spring 4 MVC -HelloWorld</title>
</head>
<body>
    <center>
        <h2>Hello World</h2>
        <h2>
            ${mvstr}   </h2>
    </center>
</body>
</html>
  • 10、发布,这里发布方式有两种,第一种就是传统的在eclipse配置tomcat然后发布,第二种是maven配置tomcat插件,这里采用第二种。我用的是tomcat8,但是pom配置仍然用7。步骤如下
** 在tomcat的conf下的tomcat-users.xml。在server字段添加 **
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
** 修改maven的setting.xml文件,增加以下内容 **
 <server>
   	<id>tomcat8</id>
   	<username>admin</username>
   	<password>admin</password>
   </server>
**  将tomcat集成到pom.xml中 **
   <plugins>
    	<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <path>/springMVCDemo</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <server>tomcat8</server>
                </configuration>
            </plugin>
    </plugins>
   1、maven每次updateProject会自动把编译级别改回1.5并且是WebModule是2.0,所以需要自行修改编译级别
   有两种方式,第一种方式修改maven的基础配置,此处不做介绍,第二种方式修改pom.xml,强制使用我们定义的编译级别,
   就是上述xml中的两处,一处是<groupId>org.apache.maven.plugins</groupId>,另一处<profiles>包围的代码片。
   2、修改完配置还要修改项目的编译级别和WebModule,先调出Navigator窗口,window->show view->Navigator,修改org.eclipse.wst.common.project.facet.core这个文件,
     <installed facet="java" version="1.8"/>
	 <installed facet="jst.web" version="3.0"/>
	还有就是.classPath中编译界别改成1.8
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
   	<attributes>
   		<attribute name="maven.pomderived" value="true"/>
   	</attributes>
   </classpathentry>
   3、在Pom.xml中,下面配置如果不加<scope>,起服务可能会报错,原因就是jar包冲突,详细原因自行百度即可。
    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <scope>provided</scope>
       <version>3.1.0</version>
   </dependency>
   4、报错:unable to create project archetype,删除工作空间所有内容,清空本地maven库,重新建立即可。
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值