1 在eclipse新建maven项目
2 选择快速构建模板
3 输入对应的groupId等等
4 新增maven架构的src目录
右击项目 new -- source folder
一共有4个,文件名分别为
src/main/java
src/main/resources
src/test/java
src/test/resources
5 更改classes路径
右击项目--properties--选择java build path--选择source选项卡
选中allow output folder for source folder
将4个目录中的output folder 的输出改成如下:
正式代码输出为:target/classes
测试代码输出为:target/classes-test
此处可以更改项目中src目录的排序,排序在order and export选项卡中
更改jdk版本在libraries选项卡中
6 把项目变更成Dynamic Web项目
右击项目--properties--project facets 点击convert to facets form
在出来的对话框中点击 dynamic web module 并在后面的version中选择2.3版本
由于之前我们配置的是编译环境是1.6,这里只能选择2.3版本了,后面版本是针对jdk1.7版本的
7 设置部署环境
右击项目--properties--这个时候属性里面多出一个 deployment assembly
将其中的test目录删除,新增maven的jar部署配置,这个地方也可以将web目录指向项目的其他地方。
8 修改pom文件,新增必要的依赖
<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>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
9 在src/main/resources文件夹中新增一个spring文件夹
在里面新增两个xml配置文件
a、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>
b、spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 使用spring模式配置链 -->
<mvc:annotation-driven />
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="com.my.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
更改web.xml文件配置
<description>SpringMVC环境抢建</description> <!-- 加载Srping配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/applicationContext.xml</param-value>
</context-param> <!-- Spring 监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- SpringMVC配置 -->
<servlet>
<servlet-name>myspringmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 自定义springmvc的配置文件名称和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- springmvc 请求后缀 -->
<servlet-mapping>
<servlet-name>myspringmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
10 新增一个测试类
package com.my.bean;
public class Animal {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.my.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.my.bean.Animal;
@Controller
public class DemoController {
@RequestMapping("/get")
public String get(Animal animal, HttpServletRequest request) {
System.out.println(animal.getId());
System.out.println(animal.getName());
request.setAttribute("animal", animal);
return "get";
}
}
测试开始jsp部分代码
<form action="/my/get" method="post" >
<input type="text" name="id" />
<input type="text" name="name" />
<input type="submit" value="提交" />
</form>
返回结果部分代码
<h1>执行返回</h1>
${animal.id}
${animal.name}
测试发现el表达式失效
更改web.xml中的配置部分
删除DOCTYPE声明部分
在web-app 后新增scheme
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
将其版本改完2.4即可
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决乱码问题
简单spring MVC配置完毕