第一步,eclipse---File---New---Maven Project
第二步,如图直接Next
第三步,如图直接Next
第四步,Group Id一般写公司域名的反转,Artifact Id写项目名称,然后点击Finish
第五步,创建好项目后,开始修改配置文件
<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.gongsi</groupId> <artifactId>xiangmu2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>xiangmu2 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <build> <finalName>xiangmu2</finalName> </build> </project>
以上是pom.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>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.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:spring-mvc.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
以上是web.xml文件
<?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.gongsi.xiangmu2"></context:component-scan> <!-- 视图页面配置 --> <!--声明视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀:视图文件的路径--> <property name="prefix" value="/WEB-INF/view/"/> <!--后缀:视图文件的扩展名--> <property name="suffix" value=".jsp"/> </bean> </beans>
以上是在/src/main/resources目录下创建的spring-mvc.xml文件
package com.gongsi.xiangmu2; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String showMessage(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("name", "xiaoHong"); return "hello"; } }
以上是在src/main/java包下创建的HelloController文件
<%@ 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>HelloWorld</title> </head> <body> <center> <h2>Hello World</h2> <h2> ${name} </h2> </center> </body> </html>
以上是在/src/main/webapp/WEB-INF下创建view文件夹,并在新创建的这个文件夹下创建hello.jsp文件
第六步,打包后生成xiangmu2.war,将它复制到tomcat项目目录下
第七步,然后启动tomcat,访问如上
标签:xml,springmvc,创建,eclipse,springframework,maven,xiangmu2,org,servlet From: https://www.cnblogs.com/qq2083587182/p/17368766.html