最近有空自学了下java,看到那些个配置,头都大了
开发工具:eclipse 版本: Oxygen.1a Release (4.7.1a)
Maven: 3.5.2
1、新建一个Maven-Project选webapp模板

groupId:com.jxm --自己任意取名
artifactId:springmvc02
version:默认就行

2、右键项目->Properties->文件格式设置成utf-8 避免以后可能的编码问题

3、建maven约定的文件结构
新建后的项目目录是这样的

按照maven的约定,应该有
src/main/java
src/main/resources
src/test/java
src/test/resources
4个source folder
新建source folder 这里碰 The folder is already a source folder的问题
解决:http://www.oschina.net/question/922518_134759
右键build path -> configure build path -> source ,选择 src/main/java、src/test/java删除,然后再新建。
接下来将JRE改成本地版本:右键JRE


回到项目 看到项目problems下有两个错误,导致项目上一直有红色的X

解决方式:
右键项目->找到build path->切换到Library->Add Library->Server Runtime

选择自己的服务器(我是tomcat,如果没有,要新建一个)

4、配置spring mvc需要的依赖包
打开 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.jxm</groupId>
<artifactId>springmvc02</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc02 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.13.RELEASE</spring.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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc02</finalName>
</build>
</project>
增加的是红色圈起来部分,因为是spring mvc 依赖 spring-webmvc包,加上之后,maven install的时候会自动加载依赖 包括 spring-core等的包

至于spring的版本,可以到 maven仓库 http://search.maven.org/ 搜索
(没用最新的)

现在可以先安装一下maven的包
右键项目->Run As->Maven Install
5、配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="study" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/configs/spring-*.xml</param-value>
</context-param>
<!-- Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义spring mvc的配置文件名称和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

上面红色部分较默认Web.xml文件有所修改,可以看到改成了2.5版本
光上面的配置文件改还不行,打开项目物理目录,找到 .settings目录的如下文件也修改成2.5版本

此时需要关掉eclipse再重新打开,使修改生效
之所以修改是因为2.3版本下 ->jsp页面的 ${hello}绑定不上值。
也可以改成更高版本的如3.1(facet文件和web.xml两者都改),则web.xml头部如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
根据web.xml中的配置,在src/main/resources下新建一个名为configs的folder
新建 spring-servlet.xml (因为文件路径已经在web.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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<!-- 启用Spring基于annotation的DI,使用户可以在Spring MVC中使用Spring的强大 功能。
激活@Required @Autowired, @PostConstruct @PreDestroy and @Resource等标注
-->
<context:annotation-config/>
<!-- 自动扫描包 -->
<context:component-scan base-package="com.jxm.mvcdemo.controller" />
<mvc:annotation-driven />
<!-- 静态资源处理,css,js,imgs -->
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />
<mvc:resources mapping="/images/**" location="/images/" />
<!-- 页面View层基本信息设定 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
新建上面配置中的包

在包下建 HelloSpringController.java
package com.jxm.mvcdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloSpringController {
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv =new ModelAndView();
mv.addObject("spring", "spring mvc");
mv.setViewName("hello"); //找views下的hello.jsp视图
return mv;
}
}
WEB-INF目录下建views文件夹,并新建hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一个Spring MVC</title>
</head>
<body>
hello ${spring}!
</body>
</html>
在tomcat下运行:右键tomcat服务器,Add and Remove

打开:http://localhost:8080/springmvc02/hello

网友评论