maven Hello World项目的构建

Posted 钟欣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven Hello World项目的构建相关的知识,希望对你有一定的参考价值。

与搭建分享一下Maven的项目构建过程:

首先,我们需要准备一个 j2EE 版本的eclipse:

eclipse下载地址:http://www.eclipse.org/downloads/

根据电脑的位数下载对应的版本即可:

maven项目构建过程:

1. 在eclipse的project Exceplorer空白处点右键,New一个maven项目

 

2. 点击Next

3.点击webapp 1.0 Next

4. 填入Group Id   Artifact Id  Version  Package 点击finish等信息如图:

 

5. 这个时候生成一个新的maven项目,项目会报错,如图:

 

 

解决办法如图:

 

6. 然后需要新建或者修改以下几个文件:

   (1) pom.xml (jar包依赖)

   (2) web.xml (基本配置)

   (3) index.jsp(需要将原来的index.xml删除,再新建一个jsp文件,因为自带的index.jsp不是标准的jsp文件)

        (4) dispatcher-servlet.xml (分发器)

   (5) HelloWorldController.java (控制器)

   (6) helloworld.jsp (页面跳转)

   这些文件在文末会有,请先看下面这张图

  最后的目录结构是下图:

 

1/6  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.programcreek</groupId>
<artifactId>HelloWorld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<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>HelloWorld</finalName>
</build>
</project>

2/6 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<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>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

3/6  index.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>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>

<center>
<h2>Hello World</h2>
<h3>
<a href="hello?name=Eric">Click Here</a>
</h3>
</center>
</body>
</html>

4/6  dispatcher-servlet.xml 

<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">
<context:component-scan base-package="com.programcreek.helloworld.controller" />
<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>

5/6 HelloWorldController.java

package com.programcreek.helloworld.controller;

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

@Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";

@RequestMapping("/hello")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");

ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}

6/6 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>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h2>
${message} ${name}
</h2>
</center>
</body>
</html>

然后访问: localhost:8088/hello会出现如下界面:

点击Click Here 会跳转页面到helloworld.jsp

至此Hello World项目搭建完毕

 

 

 

 

 

  

 

以上是关于maven Hello World项目的构建的主要内容,如果未能解决你的问题,请参考以下文章

Maven使用入门之Hello World

用maven建一个Hello World项目,maven初使用,maven如何使用

Eclipse Maven 创建Hello World Web项目

IntelliJ IDEA 创建 hello world Java web Maven项目

Spring Boot实践教程:Hello,world!

IOS系统2017版IntelliJ IDEA 创建 hello world Java web Maven项目