Spring mvc初体验
什么是Spring mvc?Spring mvc能帮我们做什么?Spring mvc的处理流程是什么?Spring mvc的优点
如果你想搞清楚这些问题,那么你就有必要好好看看https://blog.youkuaiyun.com/lplanguage/article/details/70598501这篇博客。讲的很好,也很通俗易懂。
了解工程中各个框架的作用:
到现在我们已经学习到第三个框架了,下一步就是整合这些框架到项目中去了。所以我们有必要了解一下一般工程的构造是什么样子,各个框架在项目中的具体作用。
说说我的理解吧,我觉得一个工程好像是一个饭店的内部的工作流程。首先我们需要把今天的食材进行预处理:洗菜,切菜…这就好比对数据处理(dao层负责是操作底层的数据库),然后service层就像厨师一样,他来负责将这些食材加工处理做成一道道的菜,最后通过springmvc的controller层送到每个有需要的客人桌上。等等spring去哪啦,我想原来这些层之间他们都是通过人相互喊话来进行工作的。这样的话效率着实有点低,现在我们使用spring后,其实相当于由一个电脑将这些任务分配到每个具体的层上的每个人身上。现在我么先来新建一个简单的hello world的springmvc。
主要步骤:
第一步:新建工程,导入jar包
第二步:配置web.xml文件:
这里我们要到web.xml下来进行配置,没有的同学可以右键Java EE tools然后点击Genrate Deployment Descripter Stub。这样就可以新建一个。spring是一个servlet,通过dispatcherservlet在把任务分配任务。
<!--初始化DispatcherServlet -->
<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>
</servlet>
<!-- /表示Spring MVC将捕获Web容器所有的请求,包括静态资源的请求,Spring MVC会将它们当成一个普通请求处理。 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
第三步:配置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.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 扫描所有的spring mvc controller -->
<context:component-scan base-package="com.hg.controller"/>
<!--对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,
就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->
<mvc:default-servlet-handler />
<!-- 解决了@Controller注解的使用前提配置 -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀(jsp存放的位置)-->
<property name="prefix" value="/jsp/" />
<!-- 后缀,假设要使用a.jsp配置好了只需要返回a就可以了 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
第四步:新建一个jsp
新建一个a.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 world!
</body>
</html>
第五步:配置controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/mvc")//需要使用工程名/mvc进行访问这个controller
public class MyCtroller01 {
@RequestMapping("/hello")//需要通过/hello来访问该函数。
public String hello(){
return "hello";//返回hello.jsp界面
}
}
第六步:测试:
当我们完成这个简单的spring mvc版的hello world,童鞋们是不是发现spring和spring mvc整合起来十分轻松。因为他们本是同根生,所以兄弟们"合作工作"起来默契十足。
其实不知道童鞋们是不是和我有一样的感觉:这是不是就是个servlet啊。我们使用spring mvc就来完成以下servlet的功能试试吧。
配置文件还是和上一个工程的一样的,jar包也和上一个工程是一样的,这里就不一一赘述了。
第一个实验,看看controller是怎么拿到get的值
首先我们先新建一个类PeoplePojo类
@Controller
public class MyController {
@RequestMapping("/getInfo")
public String getInfo(PeoplePojo pojo) {
System.out.println(pojo.getName() + pojo.getAge());
return "0";
}
}
测试结果:
spring mvc会根据输入类型自动封装到类中去。是不是很神奇,spring mvc还支持ant的书写风格(感兴趣的同学可以看看https://www.cnblogs.com/mohehpc/p/6476149.html)
使用request向前台传递参数:
// 使用request向前台传递参数
@RequestMapping("/test01")
public String test01(Map<String, Object> map) {
PeoplePojo pojo = new PeoplePojo();
map.put("pojo", pojo);
pojo.setAge(20);
pojo.setName("好的");
return "a";
}
再来看看我们的a.jsp是什么样子的。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ 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>
<c:out value="${pojo.name}"></c:out>
<c:out value="${pojo.age}"></c:out>
</body>
</html>
不知道童鞋们看到过这个吗,这个叫jstl标签,第一句话就是引用这种格式,当然使用他们也需要导入jar包。
使用jstl是因为jsp页面主要用于显示的,最好不要写java代码<%%>,如果不用JSTL就得写java代码。尤其是要学习前端的童鞋最好都要掌握喔。
好了我们来看看实验结果吧:
form表单提交数据
首先我们先简单的创建一个form表单:
// form表单提交数据
<%@ 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>
<form action="../test02">
姓名:<input type="text" name="userName"> 年龄:<input type="text"
name="age"> <input type="submit" value="提交">
</form>
</body>
</html>
然后我们试着拿出这些值。
// form表单提交数据
@RequestMapping("/test02")
public String test02(HttpServletRequest reuqest) {
String userName = reuqest.getParameter("userName");
String age = reuqest.getParameter("age");
System.out.println(userName + age);
return null;
}
看看测试结果吧:
session传参
// session传参
@RequestMapping("/test03")
public String test03(HttpSession httpSession) {
PeoplePojo peo = new PeoplePojo();
peo.setName("张三");
peo.setAge(10);
httpSession.setAttribute("peo", peo);
return "b";
}
新建一个b.jsp页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ 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>
<c:out value="${peo.name}"></c:out>
<c:out value="${peo.age}"></c:out>
</body>
</html>
测试结果:
cookie
实现代码:
// cookie
@RequestMapping("/test04")
public String test04(HttpServletResponse response) throws UnsupportedEncodingException {
PeoplePojo peo = new PeoplePojo();
peo.setName("TOM");
peo.setAge(10);
// Cookie cookie=new Cookie("name", URLEncoder.encode(peo.getName(),"utf-8"));
Cookie cookie = new Cookie("name", peo.getName());
Cookie cookie1 = new Cookie("age", peo.getAge() + "");
response.addCookie(cookie);
response.addCookie(cookie1);
return null;
}
测试结果:
至此我们的基本spring mvc的知识就讲解完了,由于时间比较仓促,所以这篇博客显得比较简陋。如果有什么问题的话,希望各位能够尽快告诉我,谢谢大家。
代码+jar包:MySpringMvc01+MySpringMvc02