Spring Mvc 入门之框架搭建及第一个应用程序
程序员文章站
2024-03-18 17:01:58
...
Spring Mvc 简介
1.Spring Mvc为展现层提供基于Mvc设计的优秀的web框架 (也就是说Spring Mvc 和Struts2的作用是一样的 )
2. Spring MVC是目前最主流MVC 框架之一
3.Spring3.0 后超越Struts2成为最优秀的MVC框架
4.MVC通过MVC注解,让POJO 处理请求
5.控制器无需实现任何接口
6.支持REST 风格格URL 请求
7.扩展性和灵活性
Spring Mvc 项目框架搭建
1.导入jar包
2.在web.xml中配置DispatcherServlet
3.创建springmvc.xml并且配置springmvc.xml
4.编写请求处理器并标识为控制器
5.编写视图
(1)spring mvc 需要的jar包
(2)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_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- spring监听器 加载spring Care -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置application.xml路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring Web应用程序的前端控制器,负责处理所有应用程序请求 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc.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>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(3)springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd">
<!-- 注解扫描 -->
<mvc:annotation-driven enable-matrix-variables="true"/>
<!-- 配置 spring标注
@Controller
@Service
-->
<context:component-scan base-package="com.springmvc.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 解析order 优先级 -->
<property name="order" value="1"></property>
</bean>
</beans>
(4)编写请求处理器并标识为控制器
package com.springmvc.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user")
public class SpringMvcHello {
//输入helloworld 执行了hello方法
@RequestMapping("/helloworld")
public String Hello() {
System.out.println("执行了hello方法.......");
return "success";
}
//使用Model 传值到视图页面
@RequestMapping("/1")
public String Hello1(Model model ) {
model.addAttribute("hello", "success page ........");
return "success";
}
//使用ModelAndView 传值到视图页面
@RequestMapping("/2")
public ModelAndView Hello2() {
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
mav.addObject("hello", "小猪佩奇");
return mav;
}
//使用Map 传值到视图页面
@RequestMapping("/3")
public String Hello3(Map<String , Object> model) {
model.put("hello", "biubiubiu......");
return "success";
}
}
(5)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>Insert title here</title>
</head>
<body>
${hello }
</body>
</html>
访问的链接 (springMvcHelloWorld为项目名 user为 类注解的路径 2为方法体注解的路径 )
.
上一篇: 跟着Leo机器学习实战:sklearn笔记--线性模型
下一篇: 猜数字游戏(C语言)