spring简单实例的架构关系分析

架构关系分析图
架构关系分析图分析说明:其实http服务就3个步骤,请求,处理,响应。
请求:一般就是地址或者表单,提交给服务器。
处理:一般是收到请求后,进行处理类的映射,由什么类进行处理该请求,然后对客户端的参数进行处理。
响应:一般将将输出的内容装入,jsp页面,反馈该客户端。
无了什么样的框架对http的请求,都要经过上面几个过程。那么spring mvc 的框架给我们做了什么呢?
1.首先,http我们返回会给一个地址发送数据,这些http服务器以及为我们做了。
2.这里,把数据放到{message}的过程,框架给我们做了。
3.框架的配置文件为我们做了地址和处理类的映射/hello =>hellocontrol的处理。
框架是啥,有啥价值,其实就是把一些共性的东西,重复的动作都他做了,让我们关注更少且更有价值的关注点。

1.请求地址
http://127.0.0.1:8080/HelloWeb/hello
2.处理类

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{ 
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }
}

3.http服务器配置文件(web.xml)

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

3.spring 配置文件(HelloWeb-servlet.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

4.响应文件

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

5.运行效果
spring mvc 简单实例运行效果

猜你喜欢

转载自blog.csdn.net/xie__jin__cheng/article/details/89516504