SpringMVC 1(SpringMVC的结构图,Hello 的案例)
来源:互联网 发布:gpgpu编程技术 编辑:程序博客网 时间:2024/06/06 14:27
1、MVC框架需要做的事情:
将url映射到java类或java的方法中
封装用户提交的数据
处理请求--调用相关业务--封装响应数据
将响应的数据进行渲染jsp、html、freemarker等
2、SpringMVC是一款轻量级、基于请求响应的MVC框架
3、SpringMVC的优势
简单、便捷、
和Spring可以无缝对接
约定由于配置
支持Restful风格
异常处理
拦截器等
4、SpringMVC的结构
5、Hello SpringMVC案例
1) 导入相关jar包
2) 配置web.xml文件--配置分发器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name><welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list><servlet>
<servlet-name>MVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value></init-param> <load-on-startup>1</load-on-startup></servlet> <servlet-mapping><servlet-name>MVC</servlet-name>
<url-pattern>/*.do</url-pattern></servlet-mapping> </web-app>
3)添加 SpringMVC配置文件:默认在WEB-INF下添加【dispatcherNAME-servlet.xml】文件
<?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: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"></bean> <!-- --></beans>
4) 编写HelloController代码
public class HelloController implements Controller {public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv =new ModelAndView(); //封装要显示到视图中的数据
mv.addObject("msg","Hello SpringMVC"); //视图名 mv.setViewName("hello");return mv;}}
5) 编写SpringMVC配置文件信息
<?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: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"><!-- 配置HanderMapping --><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 配置HanderAdpapter --><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置渲染器 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResol ver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><!-- 结果视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 结果视图的后缀 --><property name="suffix" value=".jsp"/>
</bean>
<!-- 配置请求和处理器 --><bean name="/hello.do" class="cn.vociecodes.controller.HelloController"/> </beans>
6) 访问路径:http://localhsot:8080/mvc/hello.do
Controller配置汇总
1、通过url配置bean
以上配置,访问/hello.do,就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统。
2、为url分配bean
此类配置还可以使用通配符,访问/hello.do时,spring会把请求分配给helloController进行处理。
3、Url匹配bean
4、注解
0 0
- SpringMVC 1(SpringMVC的结构图,Hello 的案例)
- SpringMVC的Hello World
- SpringMvc的入门案例
- springMvc的Hello world(xml配置)
- SpringMVC下面的Hello World
- SpringMVC---Hello World(1)
- Springmvc入门案例(1)
- springMVC基于注解(annotation)的Hello world
- SpringMVC 第一篇【万能的Hello World】
- Springmvc案例1----基于spring2.5的采用xml配置
- SpringMVC post请求的处理(登录表单案例)
- SpringMVC的第一个入门案例
- 基于XML配置的springMVC小案例
- 基于注解配置的springMVC小案例
- SpringMVC的第一个案例 Helloword 步骤
- springmvc整合redis的小案例
- springmvc入门案例可能出现的错误
- 基于springmvc实现上传下载的案例
- php 生成随机字符串的方法
- NSNotificationCenter addObserverForName 的释放
- Golang的...
- fopen 文本文件与二进制文件区别
- 根据不规则效果图布局方法
- SpringMVC 1(SpringMVC的结构图,Hello 的案例)
- 公钥私钥及ssh公钥无密码登录
- MySQL——常用SQL优化(一)
- Mybatis的二级缓存配置
- JVM学习笔记(五):根据字节码指令介绍方法执行流程
- php 截取某个字符之前的字符串
- Photoshop绘制炫彩的的立体圆环LOGO教程
- Android 应用程序之间内容分享详解(二)
- JVM学习笔记(六):类加载的时机