1: 采用maven进行版本管理,所以首先建立maven项目
文件-新建-maven project-artifact id 选择webapp-输入group Id 和artface Id - 点击finish
生成完成之后如下所示:
2: 添加运行时的tomcat服务器
在项目上右键-build path- configure build path-选择library标签-选择右侧的add library - server runtime - 选择安装的tomcat 服务器--finish,然后apply一下即可。
3: 将需要的依赖包加入pom 文件
4: 在resources文件夹下新建/META-INF/spring-mvc.xml文件
<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/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">
<!-- 启动注解驱动的spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<context:component-scan base-package="com.test.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" /> <!-- 前缀 -->
<property name="suffix" value=".jsp" /> <!-- 后缀 -->
</bean>
<!--这里是对静态资源的映射-->
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/img/**" location="/resources/img/" />
</beans>
5: 在src/webapp/WEB-INF/web.xml中添加配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 地址为http://localhost:8080/ 显示的默认网页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring MVC config start-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 此处指向的的是SpringMVC的配置文件 -->
<param-value>classpath:META-INF/spring-mvc.xml</param-value>
</init-param>
<!--配置容器在启动的时候就加载这个servlet并实例化-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6: 在src/java下新建com.test.controller文件夹,然后新建controller文件如下所示
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MainController {
@RequestMapping(value = "test", method = RequestMethod.GET)
public String test(){
System.out.println("I am stupid ok ok oko ko ko ko ko ko k ok o ko ko kok o");
// 实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀
return "test";
}
}
7: 在src/main/webapp/WEB-INF下新建views文件夹作为我们的目标模版的存放位置,然后新建一个test.jsp
package com.zhaolong.wsn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MainController {
@RequestMapping(value = "test", method = RequestMethod.GET)
public String test(){
System.out.println("I am stupid ok ok oko ko ko ko ko ko k ok o ko ko kok o");
// 实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀
return "test";
}
}
8: 在项目上右键run as -- run on server 启动之后 在原始地址后面输入test回车就可以看到新建的test页面了。