一、准备工作
eclipse使用maven搭建项目,项目名称为HelloSpringMVC
二、搭建好的项目如下:
项目介绍:实现简单的helloworld
三、具体代码
controller类:HelloWorldController
package com.cn.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/hello") //访问http://localhost:8080/HelloSpringMVC/hello public String hello(Model model){ model.addAttribute("greeting","hello Spring MVC"); //jsp页面接收 ${greeting} return "helloworld"; //跳转到 helloworld.jsp } }
jsp页面:helloworld.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>Spring4 MVC -HelloWorld</title> </head> <body> <h1>${greeting}</h1> </body> </html>
web项目配置:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name>HelloSpringMVC</display-name> <servlet> <!-- 加载springmvc配置: 1、这里的servlet-name默认指向“spring-mvc-servlet.xml” 2、手动指定:<init-param> --> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc-servlet.xml</param-value> </init-param> --> <!-- <load-on-startup>:容器启动时,加载这个servlet的顺序 取值:0,1,2,3... 数字越小,越先加载 --> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern:servlet访问地址url配置 /* path.endsWith("/*") *. path.starsWith("*.") / path.equals("/") 其他 --> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 加载其他的xml配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <!-- Spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
springMVC配置:spring-mvc-servlet.xml,root-context.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 自动扫描注解 --> <context:component-scan base-package="com.cn.springmvc"/> <!-- 处理静态资源,如img等,这类资源不通过DispatcherServlet转发,由容器自己处理 <mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" /> 对静态资源文件的访问 <mvc:resources mapping="/img/**" location="/img/" /> --> <mvc:default-servlet-handler /> <!-- <mvc:resources /> --> <!-- 使用注解时需要配置这个,springMVC为@Controller分发请求所必须的 --> <mvc:annotation-driven/> <!-- 视图控制器: 如果返回的是视图逻辑名称,如success,那么视图解析器就必须配置 如果返回的是真实视图名称,如/index.jsp,那么视图解析器为可选配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/" /> <!-- 前缀 --> <property name="suffix" value=".jsp" /> <!-- 后缀 --> </bean> </beans>
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.cn</groupId> <artifactId>HelloSpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HelloSpringMVC 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> <!-- Servlet Library --> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring dependencies --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>HelloSpringMVC</finalName> </build> </project>
四、访问:http://localhost:8080/HelloSpringMVC/hello
备注:简单的springmvc hello world程序,弄不好会出很多乱子,特此记录下
1、eclipse新建maven项目时,出现下面提示,新建不成功了。
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6.......
解决办法:删除maven库里面文件:C:UsersAdministrator.m2 epositoryorgapachemavenplugins这个文件夹下的所有文件
2、项目名称上的红叉,其他位置没有红叉,一般是项目构建的jdk版本太低。
3、pom.xml出现红叉,文件里并没有错误,这时选中项目update maven下,并选中Force Update of ......
4、项目没有错误,启动tomcat,报web监听包找不到,右键项目属性里的Deployment Assembly,add一个maven依赖。
5、项目没有错误,启动也没有报错,但访问时出现404,同样在4的位置,删除原先的webcontent,add一个webapp,这便是项目的真实根路径。
6、编写项目或者导入项目后,部署到tomcat找不到该项目,修改项目属性Project Facets里的Dynamic Client module为相应的版本,一般jdk1.6或以上选3.0,如果更改报错,手动找到项目源文件里的.settings文件中的org.eclipse.wst.common.project.facet.core.xml,修改jst.web版本为3.0。
7、程序没别的错误,启动疯狂报错,还是一些没见过的,建议看下项目里的buildpath,这里的4个是不是都有