发表于: 2017-12-26 22:20:41

1 712


今日完成

在网上找了一篇教程,使用spring MVC创建一个Hello World程序

1,导包

2,添加Web.xml配置文件中关于SpringMVC的配置

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>

 <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
 <servlet>
   <servlet-name>springmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springmvc-servlet.xml</param-value>
   </init-param>
   <!-- <load-on-startup>1</load-on-startup> -->
 </servlet>

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

</web-app>


3,在resources中添加springmvc-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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      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-4.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

   <!-- scan the package and the sub package -->
   <context:component-scan base-package="test.SpringMVC"/>

   <!-- don't handle the static resource -->
   <mvc:default-servlet-handler />

   <!-- if you use annotation you must configure following setting -->
   <mvc:annotation-driven />

   <!-- configure the InternalResourceViewResolver -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         id="internalResourceViewResolver">
       <!-- 前缀 -->
       <property name="prefix" value="/WEB-INF/jsp/" />
       <!-- 后缀 -->
       <property name="suffix" value=".jsp" />
   </bean>
</beans>

4,在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。index.jsp,在body中添加“Hello Spring”。

<html>
<body>
<h2>Hello World!</h2>
<h2>Hello SpringMVC!</h2>
</body>
</html>

5,编写Controller代码

package test.SpringMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/mvc")
public class MvcController
@RequestMapping("/hello")
public String hello(){
return "hello";
   }
}

6,启动服务器,键入 http://localhost:8080/项目名/mvc/hello   (为什么是404   只有输入localhost:8080才可以哎)



SpringMVC常用注解

@Controller

  负责注册一个bean 到spring 上下文中
@RequestMapping

  注解为控制器指定可以处理哪些 URL 请求
@RequestBody

  该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上 ,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上

@ResponseBody

   该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区

@ModelAttribute    

  在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法

  在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中 

@RequestParam 

  在处理方法入参处使用 @RequestParam 可以把请求参 数传递给请求方法

@PathVariable

  绑定 URL 占位符到入参
@ExceptionHandler

  注解到方法上,出现异常时会执行该方法
@ControllerAdvice

  使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常


遇到的疑惑

以上

不知道这段代码有啥用啊

不清楚上面那些配置的作用


明日计划

接着学习SpringMVC


收获

创建了一个web工程

顺便搜了下,xml中<bean>属性概述

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.            http://www.springframework.org/schema/beans/spring-beans.xsd"> 

beans —— xml文件的根节点。

xmlns ——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。

xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即/spring-beans-2.0.xsd这个文件里定义的元素遵守什么标准。

xsi:schemaLocation——是指,本文档里的xml元素所遵守的规范,schemaLocation 属性用来引用(schema)模式文档,解析器可以在需要的情况下使用这个文档对 XML 实例文档进行校验。它的值(URI)是成对出现的,第一个值表示命名空间,第二个值则表示描述该命名空间的模式文档的具体位置,两个值之间以空格分隔。



返回列表 返回列表
评论

    分享到