java mvc 案例_简单的Spring MVC经典案例

主题:构建一个基于SpringMVC的HelloWord Web 项目

目的:快速体验什么是SpringMVC

方案:

1、创建工程,命名:SpringMVC

c67c331e184f39fa7f57fdb176a1394b.png

2、导包

0b66e922b6b0ce8575cd8871496dd55c.png

3、在SRC下添加spring-mvc.xml配置文件

(注意:名字可以随便取,最好就是看上就知道是什么)

1d48b5896e48b2ef0562cd943147eff1.png

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 here

8、跑起来吧兄弟们~然后访问http://localhost/SpringMVC/hello.action,效果如下:

dbb0e612b992dac71d8fbf32e302b3cf.png

800491c1a6bf3f2a43ec81e2a28dacde.png

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值