Java web开发(10)SpringMVC使用a_SpringMVC模板程序
新建一个Maven项目,使用archetype selection,选中最后一个webapp
填写Group Id和Artifact Id.
在工程的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>org.com</groupId> <artifactId>springmvc02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc02 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>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.2.RELEASE</version> </dependency> </dependencies> <build> <finalName>springmvc</finalName> </build> </project>
此时的Maven项目没有/sr/main/java和/src/test/java等项目文件夹
解决方案:右键项目,Build Path->Configure Build Path
在Libraries选项卡中删除JRE System Library,然后Add Library,JRE System LIbrary
选中WorkSpace Default JRE (jre1.8.0_141).
配置web.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>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
这里servlet 名字是hello,然后springMVC配置会在WEB-INF文件夹下搜索hello-servlet.xml文件
所以在WEB-INF文件夹下创建spring-servlet.xml文件,
输入如下内容,注意.xsd文件的需要版本号,并且和pom.xml中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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置扫描的包 --> <context:component-scan base-package="com.springdemo.*" /> <!-- 注册HandlerMapper、HandlerAdapter两个映射类 --> <mvc:annotation-driven /> <!-- 访问静态资源 --> <mvc:default-servlet-handler /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
在WEB-INF文件夹下创建view文件夹,里面创建demo.jsp文件
<%@ 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>首页</title>
</head>
<body>
<h1>This is SpringMVC Demo第一个springMVC项目</h1>
</body>
</html>
创建类DemoController,包名com.springdemo.controller
package com.springdemo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/demo") public class DemoController { @RequestMapping("/index") public String index(){ return "demo"; } }