完整代码:helloworld.zip" style="line-height: 16px; font-size: 12px; color: rgb(0, 102, 204);">lx01_helloworld.zip


步骤:

创建maven项目,选择webapp

blob.png

创建后有错误,这是缺少servlet导致,一会儿编辑pom.xml引用servlet.jar就能解决

blob.png

配置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.shuoeasy</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>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		<spring.version>4.3.1.RELEASE</spring.version>
		<servlet.version>3.1.0</servlet.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</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>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>helloworld</finalName>
	</build>
</project>


接下来对项目进行一个设置,防止包变成文件夹来显示。项目鼠标右键->Build Path->Config Build Path->选择Source选项卡,按截图取消Excluded:**配置

blob.png


编辑web.xml,可以用插件快速填充基础配置

blob.png

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	
	<!-- 快捷方式:Ctrl+/,选择 #dispatcherServlet -->
	
	<!-- 配置 DispatcherServlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置 DispatcherServlet 初始化参数 ,知道springmvc的配置文件路径和文件名-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<!-- 启动时加载 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 接收请求的路径 -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping> 
</web-app>

springmvc.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-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.shuoeasy.helloworld"></context:component-scan>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<!-- 文件名 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	<h2>Hello World !</h2>
	<a href="show" target="_blank">打开一个mvc页面</a>
</body>
</html>

控制器 Home.java:

package com.shuoeasy.helloworld;

import javax.servlet.http.HttpServletRequest;

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

@Controller
public class Home {
	
	@RequestMapping("/show")	 
	public String showPage(HttpServletRequest request){
		System.out.println("接收到页面请求");
		return "home_index";
	}
}

视图 home_index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
	我是mvc页面!
</body>
</html>


配置tomcat,Window->Preferences:

blob.pngblob.png


运行项目:

项目右键,Run as-> Run on Server

blob.png

正常运行了:

blob.pngblob.png

你可能感兴趣的文章