主题:构建一个基于SpringMVC的HelloWord Web 项目
目的:快速体验什么是SpringMVC
方案:
1、创建工程,命名:SpringMVC
2、导包
3、在SRC下添加spring-mvc.xml配置文件
(注意:名字可以随便取,最好就是看上就知道是什么)
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
4、在web.xml配置封装在Spring里面的servlet--DispatcherServlet前端控制器,并指定spring-mvc.xml文件
SpringMVC
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
SpringMVC
*.action
5、在spring-mvc.xml中配置 【HandlerMapping组件】------------------作用------>设置客户端请求与Controller
【InternalResourceViewResolver组件】--作用------>设置视图配置
【HelloController】------------------------作用------->测试请求处理
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
helloController
6、编写HelloController【注意:需要实现Controller接口】
packagecontroller;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.web.servlet.ModelAndView;importorg.springframework.web.servlet.mvc.Controller;public class HelloController implementsController{
@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throwsException {
ModelAndView mv= new ModelAndView("hello");
System.out.println("处理hello.action请求");returnmv;
}
}
7、在WEB-INF文件夹下新增"jsp"文件夹,并添加hello.jsp
Insert title here8、跑起来吧兄弟们~然后访问http://localhost/SpringMVC/hello.action,效果如下: