Spring MVC小白入门笔记(一):HelloWorld

Spring web MVC框架提供了MVC(模型 - 视图 - 控制器) 架构和用于开发灵活和松散耦合的Web应用程序的组件。

 MVC模式导致应用程序的不同方面(输入逻辑,业务逻辑和UI逻辑)分离,同时提供这些元素之间的松散耦合。

·        模型(Model)封装了应用程序数据,通常它们将由POJO类组成。

·        视图(View)负责渲染模型数据,一般说生成客户端浏览器可以解HTML输出。

·        控制器(Controller)负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染。

第一步:项目编译

这里我没有不使用jar包,而是使用的Gradle,

1.把Gradle文件复制到项目层。

2.添加Gradle Nature

 

3.Refresh Gradle Project

4.项目编译设置 点击window-preferences弹出一个窗口,如图2 ,点击Java Build Path,完成图2 的设置


图一


图2

5.项目编译设置,在搜索栏搜索deployment assembly,如图3,点击add,弹出图4 的窗口,选择Folder,添加文件。


图三


图4


图5 ,最终结果

第二步 必须的配置

需要使用web.xml文件中的URL映射来映射希望DispatcherServlet处理的请求

<?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>springmvc</display-name>
	<!-- 配置DispatchcerServlet -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- Spring mvc配置文件的位置和名称 -->
		<!--配置初始化参数:作用是配置SpringMVC配置文件的位置和名称-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>  <!-- 所有的的请求,都会被DispatcherServlet处理 -->
	</servlet-mapping>
	<filter>
		<filter-name>EncodingFilter</filter-name> <!--编码过滤器-->
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
</web-app>

spring-mvc.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.0.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">          
     
    <!-- 配置自动扫描的包 -->  
    <context:component-scan base-package="com.cn.controller"></context:component-scan>  
    <mvc:annotation-driven />       
         
    <!-- 配置视图解析器  把handler 方法返回值解析为实际的物理视图 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name = "prefix" value="/WEB-INF/views/"></property>  
        <property name = "suffix" value = ".jsp"></property>  
    </bean>         
</beans>  

第三步 编写代码

1.编写类HelloWorld

package com.cn.controller;

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

/**
 * 1. 使用 @RequestMapping 注解来映射请求的URL(相当于web.xml中的servlet-mapping元素的url-pattern)
 * 2. 返回值会通过视图解析器解析为实际的物理视图,

 */
@Controller
public class HelloWorld {

	
		@RequestMapping("/helloworld")
		public String success() {
			
			return "success";
		}
}

二.编写视图层的页面,这里就两个简单的页面,一个是index.jsp页面,一个是success.jsp页面

(一)index.jsp页面,路径是 /index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC</title>
</head>
<body>
	<h1>
		<a href="helloworld">HelloWorld</a>
	</h1>
</body>
</html>
(二)success.jsp页面,路径是 /WEB-INF/views/success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SpringMVC HelloWorld</title>
</head>
<body>
	<h1>Welcome</h1>
</body>
</html>

最后一步,把项目部署到tomcat

启动Tomcat,右击Tomcat,点击add and remove,添加你想要添加的项目

web结构



最后在浏览器输入http://localhost:8080/springmvc1,如下图




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值