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.doBean,此类方式仅适用小型的应用系统。

2、url分配bean


此类配置还可以使用通配符,访问/hello.do时,spring会把请求分配给helloController进行处理。

3、Url匹配bean


4、注解










0 0
原创粉丝点击