SpringMVC—基于注解/非注解方式下的Controller
首先声明一下:程序主要作用是把登录信息带到显示界面
一:基于非注解方式下的Controller:
1.搭建环境项目
准备好项目所需要的jar包,搭建好的项目结构如下:
相关的jar:
2.修改配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置非注解方式下的处理器的配置,映射“/register”请求-->
<bean name="/register" class="com.hang.controller.RegisterController"/>
<!-- 视图解析器 -->
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀-->
<property name="prefix" value="/"/>
<!-- 配置后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.修改Controller类
public class RegisterController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//非注解
String uname = httpServletRequest.getParameter("uname");
String pwd = httpServletRequest.getParameter("pwd");
String age = httpServletRequest.getParameter("age");
//怎么样把请求数据全部显示在界面->借助一集合
ArrayList arrayList=new ArrayList();
arrayList.add(uname);arrayList.add(pwd);arrayList.add(age);
return new ModelAndView("showRegisterInfo","datalist",arrayList);
}
}
4.测试
register.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>登录页面</title>
</head>
<body>
<form action="register" method="post">
用户名:<input type="text" name="uname"/><br/>
密码:<input type="text" name="pwd"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" name="btn1" value="提交"/>
<input type="reset" name="btn2" value="重置"/>
</form>
</body>
</html>
showRegisterInfo.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>显示信息界面</title>
</head>
<body>
<c:forEach items="${datalist }" var="data">
${data }
</c:forEach>
</body>
</html>
测试结果
二:基于注解方式下的Controller:
1.搭建环境
和上述环境一样,就不在赘述
2.修改配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置注解扫描包:扫描包下的带注解的控制器-->
<context:component-scan base-package="com.hang.controller"/>
</beans>
3.修改Controller类
package com.hang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
@Controller
//1.添加@Controller(对应生成bean的配置)
public class MyController {
//在类里面的方法上加上映射请求
@RequestMapping("/register2")
public ModelAndView register(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
//接收请求数据
String uname = arg0.getParameter("uname");
String pwd = arg0.getParameter("pwd");
String age = arg0.getParameter("age");
ArrayList list = new ArrayList();
list.add(uname);
list.add(pwd);
list.add(age);
return new ModelAndView("showRegisterInfo", "datalist", list);
}
}
4.测试
这里我们用register2.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>登录页面</title>
</head>
<body>
<form action="register2" method="post">
用户名:<input type="text" name="uname"/><br/>
密码:<input type="text" name="pwd"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" name="btn1" value="提交"/>
<input type="reset" name="btn2" value="重置"/>
</form>
</body>
</html>
showRegisterInfo.jsp和上述文件代码一致,不再赘述
测试结果
拓展:关于SpringMVC的整体架构和执行流程(转载)
这是转载一些其他博文的图片,参考请见文章末尾
1、 用户发起请求到前端控制器(DispatcherServlet),前端控制器没有能力处理业务逻辑;
2、 通过HandlerMapping查找模型(Controller、Handler);
3、 返回执行链,执行链包含了2部分内容,Handler对象以及拦截器(组);
4、 通过HandlerAdapter执行模型(Handler)
5、 适配器调用Handler对象处理业务逻辑;
6、 模型处理完业务逻辑,返回ModelAndView对象,view不是真正的视图对象,而是视图名称;
7、 将ModelAndView对象返回给前端控制器;
8、 前端控制器通过视图名称经过视图解析器查找视图对象;
9、 返回视图对象;
10、前端控制器渲染视图;
11、返回给前端控制器;
12、前端控制器将视图(html、json、xml、Excel)返回给用户;
使用springMVC必须配置的三大件:
处理器映射器、处理器适配器、视图解析器
通常,我们只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可,而省去了大段的xml配置
也就是说我们在配置springmvc-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 过滤静态资源,比如.css,.js等-->
<mvc:default-servlet-handler/>
<!-- 开启注解驱动-->
<mvc:annotation-driven/>
<!-- 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀-->
<property name="prefix" value="/"/>
<!-- 配置后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置注解扫描包-->
<context:component-scan base-package="com.hang.controller"/>
</beans>
原文链接:https://blog.csdn.net/weixin_42529699/article/details/88085405