SpringMVC +Maven

xiaoxiao2021-02-28  29

1.环境准备。

jdk   1.8 myeclipse 8.6 maven-3.3.9    maven 下载地址:http://maven.apache.org/download.cgi apache-tomcat-8.5.16 配置java 环境变量。。。此处略过。 配置mavne环境变量。 此处以我本机为例,MAVEN_HOME=D:\maven\apache-maven-3.3.9   path=%maven_home%\bin; tomcat 环境变量  tomcat_HOME=D:\apache\apache-tomcat-9.0.0.M21-src    path=%tomcat_HOME%\bin    classpath =%tomcat_HOME%\lib\jmf.jar; 安装自己的maven环境。 配置本机是setting.xml 文件。 配置tomcat  tomcat 运行的jdk 使用1.8 现在我们在myeclipse 中可以建立一个普通的maven 项目   mavenTest 然后添加web 关联 建立web需要目录 web_inf 文件目录 填写相应的webRoot Directory  我本机是src\main\webapp      Web Context Root   一般为项目名称   MavenDemo 配置 pom.xml 所需要的jar包  路径:D:\project\MavenTest\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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>MavenTest</groupId> <artifactId>MavenTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MavenTest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>1.2_04</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_04</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- <dependency> <groupId>com.*.slc</groupId> <artifactId>*-slc-common</artifactId> <version>1.0.0</version> </dependency> --> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>1.5.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.objenesis</groupId> <artifactId>objenesis</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> </dependencies> </project> 配置dispatchServlet-server.xml  路径:D:\project\MavenTest\src\main\webapp\WEB-INF\dispatchServlet-server.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.1.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"> <!-- 自动扫描的包名 --> <context:component-scan base-package="com.Ace.controller"></context:component-scan> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven /> <!-- 视图解释类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Views/" /> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name="suffix" value=".jsp" /> </bean> </beans> hello.java 文件  package com.Ace.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class Hello {     //hello world     @RequestMapping(value = "/hello")     public String hello() {         System.out.println("spring mvc hello world!");         return "hello";     }     @RequestMapping(value = "/hello2")     public String hello2() {         System.out.println("spring mvc hello world!");         return "hello2";     }     //hello world     @RequestMapping(value = "/ok")     @ResponseBody     public Object ok() {         System.out.println("ok");         List<String> list = new ArrayList<String>();         list.add("1111111111111111111111111");         list.add("2222222222222");         list.add("33333333333333");         list.add("444444444444444");         list.add("555555555555");         list.add("6666666666666");         list.add("777777777777");         list.add("88888888888");         list.add("9999999999999999");         return list;     } } hello.jsp 文件 hello.jsp <%@ page language="java" contentType="text/html; charset=UTF8" pageEncoding="UTF8"%> <!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=UTF8"> <title>Insert title here</title> </head> <body> hello world,gogogo!1111111111111111111111111111111 </body> </html> tomcat添加项目并启动,成功!!!
转载请注明原文地址: https://www.6miu.com/read-600297.html

最新回复(0)