文章目录
- 1.封装请求数据
- 1.将方法的httpservletrequest和httpservletresponse参数封装到参数数组进行反射调用
- 1.修改SunDispatcherServlet.java的executeDispatch方法
- 2.debug测试
- 2.封装http请求参数
- 1.需求分析
- 2.自定义注解RequestsParam
- 3.修改MonsterService接口,添加方法
- 4.修改MonsterServiceImpl.java添加方法
- 5.修改SunDispatcherServlet.java编写方法根据请求参数的名字判断对应于形参列表的哪个位置
- 6.修改SunDispatcherServlet.java的executeDispatch方法
- 7.单元测试
- 8.通过参数名匹配
- 1.修改SunDispatcherServlet.java的executeDispatch方法
- 2.添加方法
- 3.引入插件pom.xml否则getName得到的形参列表是下面的[arg0, arg1, arg2]
- 4.单元测试
- 2.完成视图解析
- 1.需求分析
- 2.框架搭建
- 1.编写Service
- 1.MonsterService.java
- 2.MonsterServiceImpl.java添加方法
- 2.编写MonsterController.java添加方法
- 3.编写login.jsp
- 4.login_ok.jsp
- 5.login_error.jsp
- 6.修改SunDispatcherServlet.java解决中文乱码问题
- 3.完成基本视图解析器
- 1.修改SunDispatcherServlet.java
- 2.单元测试
- 4.自定义ResponseBody注解
- 1.需求分析
- 2.编写注解ResponseBody.java
- 3.编写Controller,MonsterController.java
- 4.引入Json格式转换工具jackjson
- 5.修改SunDispatcherServlet.java,在executeDispatch方法添加逻辑
- 6.单元测试
- 5.SpringMVC框架总结
1.封装请求数据
1.将方法的httpservletrequest和httpservletresponse参数封装到参数数组进行反射调用
1.修改SunDispatcherServlet.java的executeDispatch方法
private void executeDispatch(HttpServletRequest request, HttpServletResponse response) {
SunHandler sunHandler = getSunHandler(request);
if (sunHandler != null) {
try {
Class<?>[] parameterTypes = sunHandler.getMethod().getParameterTypes();
Object[] objects = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
if ("HttpServletRequest".equals(parameterTypes[i].getSimpleName())) {
objects[i] = request;
}
if ("HttpServletResponse".equals(parameterTypes[i].getSimpleName())) {
objects[i] = response;
}
}
sunHandler.getMethod().invoke(sunHandler.getController(),objects);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
try {
response.getWriter().write("<h1>404 not found!</h1>");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2.debug测试
2.封装http请求参数
1.需求分析
2.自定义注解RequestsParam
package com.Sun.sunspringmvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestsParam {
String value() default "";
}
3.修改MonsterService接口,添加方法
4.修改MonsterServiceImpl.java添加方法
public List<Monster> findMonstersByName(String name) {
ArrayList<Monster> monsters = new ArrayList<Monster>();
monsters.add(new Monster(1, "牛魔王", "芭蕉扇", 500));
monsters.add(new Monster(2, "蜘蛛精", "吐口水", 200));
monsters.add(new Monster(3, "蜘蛛精", "吐口水", 200));
monsters.add(new Monster(4, "蜘蛛精", "吐口水", 200));
monsters.add(new Monster(5, "蜘蛛精", "吐口水", 200));
ArrayList<Monster> monsters1 = new ArrayList<Monster>();
for (Monster monster : monsters) {
if (monster.getName().equals(name)) {
monsters1.add(monster);
}
}
return monsters1;
}
5.修改SunDispatcherServlet.java编写方法根据请求参数的名字判断对应于形参列表的哪个位置
public int getIndexRequestParameterIndex(Method method, String name) {
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
if (parameter.isAnnotationPresent(RequestsParam.class)) {
RequestsParam annotation = parameter.getAnnotation(RequestsParam.class);
if (annotation.value().equals(name)) {
return i;
}
}
}
return -1;
}
6.修改SunDispatcherServlet.java的executeDispatch方法
7.单元测试
8.通过参数名匹配
1.修改SunDispatcherServlet.java的executeDispatch方法
2.添加方法
public List<String> getParameterNames(Method method) {
List<String> parNames = new ArrayList<String>();
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
String name = parameter.getName();
parNames.add(name);
}
return parNames;
}
3.引入插件pom.xml否则getName得到的形参列表是下面的[arg0, arg1, arg2]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<encoding>utf-8</encoding>
</configuration>
</plugin>
4.单元测试
2.完成视图解析
1.需求分析
2.框架搭建
1.编写Service
1.MonsterService.java
2.MonsterServiceImpl.java添加方法
@Override
public boolean login(String name) {
if ("sun".equals(name)) {
return true;
}
return false;
}
2.编写MonsterController.java添加方法
@RequestMapping("monster/login")
public String login(String mName) {
System.out.println("接收到的名字=" + mName);
boolean login = monsterService.login(mName);
if (login) {
return "forward:/login_ok.jsp";
} else {
return "forward:/login_error.jsp";
}
}
3.编写login.jsp
<%--
Date: 2024/2/28
Time: 13:54
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="get" action="monster/login">
姓名:<input name="mName" type="text"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
4.login_ok.jsp
<%--
Date: 2024/2/28
Time: 13:56
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功</h1>
欢迎你:${?}
</body>
</html>
5.login_error.jsp
<%--
Date: 2024/2/28
Time: 13:56
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录失败</h1>
欢迎你:${?}
</body>
</html>
6.修改SunDispatcherServlet.java解决中文乱码问题
3.完成基本视图解析器
1.修改SunDispatcherServlet.java
if (result instanceof String) {
if (((String) result).contains(":")) {
String[] split = ((String) result).split(":");
String type = split[0];
String name = split[1];
if (type.equals("forward")) {
try {
request.getRequestDispatcher(name).forward(request, response);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (type.equals("redirect")) {
String fill = "/sun_springmvc";
try {
response.sendRedirect(fill + name);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} else {
try {
request.getRequestDispatcher("/" + result).forward(request, response);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2.单元测试
4.自定义ResponseBody注解
1.需求分析
2.编写注解ResponseBody.java
package com.Sun.sunspringmvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}
3.编写Controller,MonsterController.java
@ResponseBody
@RequestMapping(value = "monster/list/json")
public List<Monster> listMonsterByJson(HttpServletRequest request, HttpServletResponse response) {
List<Monster> monsters = monsterService.listMonsters();
return monsters;
}
4.引入Json格式转换工具jackjson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
5.修改SunDispatcherServlet.java,在executeDispatch方法添加逻辑
else if (result instanceof ArrayList) {
Method method = sunHandler.getMethod();
if (method.isAnnotationPresent(ResponseBody.class)) {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(result);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write(json);
writer.flush();
writer.close();
}
}
6.单元测试
5.SpringMVC框架总结