Spring MVC学习笔记:helloworld的实现+@RequestMapping 的使用(上集)

日期:2017/12/5

     随着不断地深入,算是对springmvc架构的使用有了基础的理解,然后做个小总结。

    Spring MVC原理(大牛总结的经典解耦方案):


图1



图2


(1)首先理解的是Front Controller(前端控制器):

    1.1 作用:接收了用户的请求(url),然后通过requestmapping将其分配给特定的controller进行处理;

    1.2 本质--DsipatcherServlet,它继承了HttpServlet这个抽象类。为了使用它,你需要在你的web应用程序中的web.xml中配置一下,配置如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!-- 配置 DispatcherServlet -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置 DispatcherServlet的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
		<!-- 实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
			默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml-->
		<!--  
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		-->
		<!--容器启动时就被加载了 	 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

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

</web-app>


      还不够,DispatcherServlet-sevlet.xml 也要有内容才行!如下:


<?xml version="1.0" encoding="UTF-8"?>

<!--  DispatcherServlet是前置控制器,配置在web.xml文件中的。
	拦截匹配的请求,Servlet拦截匹配规则要自已定义,
	把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步。
 -->
<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.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


	<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
	<!--prefix 前缀+suffix 后缀  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/mmb-views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>		

</beans>


(2)Controller(逻辑控制器)


      写一个helloworld.java,要按照mvc的规则去编码,如下:


package com.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class helloworld {
	@RequestMapping("helloworld")
	public String helloworld(){
		System.out.println("hello world");
		return "success";
	}
}

(3)写一个 视图页面success.jsp,注意文件存放位置:

            /WebContent/WEB-INF/xxx/xxx/success.jsp


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<!-- 点击href,就会发送helloworld的请求 -->
	<a href="helloworld">Hello World</a>
</body>
</html>

(4)申明前提:

          4.1 你已经安装了 javaee eclipse mars、JDK-EE、tomcat v8.0/7.0;

          4.2 在软件内配置好了Server,建立了一个Dynami Web Project,并且导入了jar包(最好下面8个),这样可以确保运行环境是认真的,当然过程还是小坑不断。



(5)上面运行效果:









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值