1.创建Spring工程
选择最下方的Download,系统会自动下载一些需要的jar包
项目名可以自取
Finish之后进入下图页面,没什么用直接close就好
创建好的工程如下图所示
2.搭建Tomcat
到此Tomcat就配置好了
3.配置Controller及jsp相关文件
先创建一个包
在包中创建Controller文件
package com.jd;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello.do")
public String hello(){
return "index";
}
}
将web.xml中改为右图
4.配置jar包
选择右下角的fix
完成后点击Ok
5.配置dispatcher-servlet.xml文件
一定要手动输入,输入过程中最上方的beams标签的xmlns属性会自动配置,否则会报错
<?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/cache"
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/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.jd"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
6.运行
7.报错
可能会出现错误
No bean named 'cacheManager' available
这是由于IDEA自动配置了cache,但配置文件中又没有指定缓存的空间,JVM虚拟机中有cacheManage,导致spring进行bean管理时扫描到两个cache Manage,且二者都不能正常加载,出错
改法:
8.再次启动
在浏览器搜索框中输入地址即可正确跳转