准备:
Eclipse、Tomcat、JDK
开始:
新建一个动态web项目,选择 Dynamic Web Project 点击 NEXT 填写项目名称,点NEXT NEXT 选中Generate web.xml 选项,结束。
在项目下建立相关文件夹,包括java,resources(存放spring等资源文件),view(存放动态页面),右键点击项目,修改properties -> sources 为java和resources(新建资源文件夹)
引入相关jar包,如下:
web.xml
eonhr
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext-web.xml
apiServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
apiServlet
/
view/index.jsp
404
/WEB-INF/view/error/404.jsp
src/resources下新建xml文件
1、applicationContext-web.xml
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
2、springMVC.xml
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
到上面,简单的环境已经搭建好了。
测试:
建立controller:
packagecom.eon.hr.test;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")public classTestAction {
@RequestMapping(value="byebye")publicString byeTest(){return "/test/test_bye";
}
}
返回页面 /test/test_bye.jsp:
test首页index.jsp:
Indexbye
访问:
http://127.0.0.1:8080/eonhr/
DEMO:点击查看