Spring随笔之使用Idea创建SpringMvc项目Hello World
首先我们先使用配置文件的方式创建一个Spring项目。
大家在学习的时候不要图一时方便直接使用idea依赖maven一键式创建,那样其中的很多配置细节可能会注意不到,特别是对spring配置不熟悉的朋友,很不利于学习。而且希望在一次次创建spring的过程中希望每次都能提升对spring的认识。
好的言归正传,我这里使用的intellij idea创建spring项目。intellij idea 是一个好工具,希望还在使用eclipse的朋友切换过来,一方面是这个软件很强大,另一方面现在是微服务和分布式的时代。
第一步:下载intellij idea
https://www.jetbrains.com/idea/
第二步:安装
省略。。。
第三步:**
百度吧。国人很强大!啥都有**版的!
第四步:创建项目
新创建spring项目,这里我们使用Springmvc当前默认的是4.3.18版本下面configure可以选择其他版本,我用默认的就行了,直接next就好了。
这里是项目名称和存放位置,项目名称就hello吧。然后finish等待创建。这期间他会自动下载spring依赖包,等下就好了。
创建完成后我们看下目录,我这里改了下项目名HelloDemo,规范点。
第五步:加入tomcat服务器
添加tomcat 在deployment中添加项目war文件
第六步:开始配置Spring相关文件
首先是web.xml,主要用来启动框架分发请求等等,下面是idea自动生成的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
然后配置applicationContext.xml
首先我们先创建一个包com.oldma.test,和一个TestController
然后配置Spring扫描等相关内容,命名空间一定别漏 否则会有各种错误
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<!-- SpringMVC只负责controller相关的Bean的注册 -->
<context:component-scan base-package="com.oldma.test" />
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
第七步:编写代码
好的 然后我们创建一个hello.jsp 和控制器的内容
TestController.java
package com.oldma.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class TestController {
@RequestMapping("/index.do")
public String getIndex(){
return "hello";
}
}
以及hello.jsp 内容随便。
启动tomcat,浏览器输入 localhost:8080/index.do
成功!
注:在创建项目时启动时可能碰到
1,org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
这个tomcat6的问题,用7以及以上就可以了
2,ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
是spring没有引入 在project structure中fix就行了