• j2ee 自定义MVC(上)


    目录

    1.自定义MVC实现计算器 

    1.配置文件

     2.实体类

     3.utils工具类

     4.Action类(层)

     2.自定义MVC模拟注册功能

    1.实体类

     2.utils工具类

     3.Action类


    1.自定义MVC实现计算器 

    1.配置文件

    • config.xml文件 (放在src包下即可)

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!-- 根目录 -->
    3. <config>
    4. <!-- 第一个action标签 -->
    5. <!--indexAction-->
    6. <action path="/indexAction" type="com.jmh.action.IndexAction">
    7. <forward name="failed" path="/add.jsp" redirect="false" />
    8. <forward name="success" path="/add.jsp" redirect="true" />
    9. </action>
    10. <!--userAction-->
    11. <action path="/userAction" type="com.jmh.action.UserAction">
    12. <forward name="failed" path="/add.jsp" redirect="false" />
    13. <forward name="success" path="/add.jsp" redirect="true" />
    14. </action>
    15. </config>
    • web.xml配置文件
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <!--中央控制器-->
    7. <servlet>
    8. <servlet-name>ActionServlet</servlet-name>
    9. <servlet-class>com.jmh.framework.ActionServlet</servlet-class>
    10. <!--配置、配置文件-->
    11. <init-param>
    12. <param-name>config</param-name>
    13. <param-value>/config.xml</param-value>
    14. </init-param>
    15. </servlet>
    16. <servlet-mapping>
    17. <servlet-name>ActionServlet</servlet-name>
    18. <url-pattern>*.action</url-pattern>
    19. </servlet-mapping>
    20. </web-app>

     2.实体类

    • Index.java
    1. package com.jmh.entity;
    2. import java.io.Serializable;
    3. /**
    4. * index对象
    5. */
    6. public class Index implements Serializable {
    7. private float sa;
    8. private float sb;
    9. public Index() {
    10. }
    11. public Index(float sa, float sb) {
    12. this.sa = sa;
    13. this.sb = sb;
    14. }
    15. public float getSa() {
    16. return sa;
    17. }
    18. public float getSb() {
    19. return sb;
    20. }
    21. public void setSa(float sa) {
    22. this.sa = sa;
    23. }
    24. public void setSb(float sb) {
    25. this.sb = sb;
    26. }
    27. @Override
    28. public String toString() {
    29. return "Index{" +
    30. "sa=" + sa +
    31. ", sb=" + sb +
    32. '}';
    33. }
    34. }

     3.utils工具类

    • ActionServlet.java
    1. package com.jmh.framework;
    2. import org.apache.commons.beanutils.BeanUtils;
    3. import org.dom4j.DocumentException;
    4. import javax.servlet.ServletConfig;
    5. import javax.servlet.ServletException;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.lang.reflect.InvocationTargetException;
    11. import java.lang.reflect.Method;
    12. import java.util.Enumeration;
    13. import java.util.HashMap;
    14. import java.util.Map;
    15. import java.util.Set;
    16. /**
    17. * 中央控制器
    18. */
    19. public class ActionServlet extends HttpServlet {
    20. //定义configModel对象
    21. private ConfigModel configModel;
    22. //定义默认配置文件.xml
    23. private String path="/config.xml";
    24. @Override
    25. public void init(ServletConfig config) throws ServletException{
    26. try {
    27. //获取配置文件key
    28. String path = config.getInitParameter("config");
    29. //判断key如果为空就赋默认值
    30. if(null==path){
    31. //初始化方法、读取配置文件
    32. configModel= ConfigModelFactory.bind(this.path);
    33. }else{
    34. //key里面有值
    35. configModel= ConfigModelFactory.bind(path);
    36. }
    37. } catch (DocumentException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. @Override
    42. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    43. doPost(req, resp);
    44. }
    45. @Override
    46. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    47. //设置编码
    48. req.setCharacterEncoding("utf-8");
    49. resp.setContentType("text/html;charset=utf-8");
    50. System.out.println("来到了中央控制器");
    51. //获取请求路径
    52. String servletPath = req.getServletPath();
    53. //获取路径最后一个点的下标
    54. int in = servletPath.lastIndexOf(".");
    55. //开始截取 从下标0到获取最后一个点的下标
    56. String path = servletPath.substring(0, in);
    57. try {
    58. //根据路径获取ActionModel对象
    59. ActionModel actionModel = this.createActionModel(path, configModel);
    60. //通过ActionModel对象获取全限定名实例化Action对象
    61. Action action = this.createAction(actionModel.getType());
    62. System.out.println("反射实例化==="+action);
    63. //开始调用给属性赋值的方法
    64. this.getModels(action,req);
    65. //调用方法
    66. String code = action.query(req, resp);
    67. //跳转是否转发、重定向方法
    68. this.reqresp(code,actionModel,req,resp);
    69. } catch (Exception e) {
    70. e.printStackTrace();
    71. }
    72. }
    73. /**
    74. * 根据.action路径获取ActionModel对象
    75. * @param path 路径
    76. * @param con configMOdel对象
    77. * @return ActionModel对象
    78. */
    79. private ActionModel createActionModel(String path,ConfigModel con){
    80. ActionModel action = con.pop(path);
    81. //非空
    82. if(null==action){
    83. throw new RuntimeException(path+"找不到");
    84. }
    85. //反之
    86. return action;
    87. }
    88. /**
    89. * 通过全限定名实例化Action对象
    90. * @param type 全限定名
    91. * @return
    92. * @throws ClassNotFoundException
    93. * @throws IllegalAccessException
    94. * @throws InstantiationException
    95. */
    96. private Action createAction(String type){
    97. try {
    98. if(null==type){
    99. throw new RuntimeException("传来的全限定名为空");
    100. }
    101. //获取类对象
    102. Class clazz = Class.forName(type);
    103. //通过类对象实例化对象
    104. Action action = (Action) clazz.newInstance();
    105. return action;
    106. }catch (Exception e){
    107. throw new RuntimeException(e);
    108. }
    109. }
    110. //获取返回值判断是转发还是重定向
    111. private void reqresp(String code,ActionModel a,HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException {
    112. //判断返回参数是否为空
    113. if(null==code||"".equals(code)){
    114. throw new RuntimeException("返回值为空");
    115. }
    116. //获取forward对象里面是否跳转路径
    117. ForwardModel forward = a.pop(code);
    118. //判断对象是否为空
    119. if(null==forward){
    120. throw new RuntimeException(code+"对应的对象找不到");
    121. }
    122. //判断是否跳转
    123. boolean redirect = forward.isRedirect();
    124. if(redirect){
    125. //为true重定向
    126. resp.sendRedirect(req.getContextPath()+forward.getPath());
    127. }else{
    128. //反之 转发
    129. req.getRequestDispatcher(forward.getPath()).forward(req,resp);
    130. }
    131. }
    132. /**
    133. * 给对象属性赋值
    134. */
    135. private void getModels(Action action,HttpServletRequest req) throws InvocationTargetException, IllegalAccessException {
    136. //判断对象是否实现了接口
    137. if(action instanceof ModelDriver){
    138. //实现了就强转
    139. ModelDriver model=(ModelDriver)action;
    140. //调用方法、给属性赋值
    141. Object model1 = model.getModel();
    142. //获取所有的请求参数
    143. Map<String, String[]> map = req.getParameterMap();
    144. Set<Map.Entry<String, String[]>> entries = map.entrySet();
    145. for(Map.Entry<String, String[]> str:entries){
    146. //System.out.println(str.getKey()+"《===》"+str.getValue());
    147. }
    148. BeanUtils.populate(model1,map);
    149. }
    150. }
    151. }
    •  Action.java(抽象类)
      1. package com.jmh.framework;
      2. import javax.servlet.ServletException;
      3. import javax.servlet.http.HttpServletRequest;
      4. import javax.servlet.http.HttpServletResponse;
      5. import java.io.IOException;
      6. /**
      7. * 子控制器的父类
      8. */
      9. public abstract class Action {
      10. //定义一个抽象方法
      11. public abstract String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, Exception;
      12. }
    •  DisPatAction.java
    1. package com.jmh.framework;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. import java.lang.reflect.Method;
    7. import java.util.Map;
    8. import java.util.Set;
    9. /**
    10. * 处理相识方法的功能
    11. */
    12. public class DisPatAction extends Action{
    13. @Override
    14. public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, Exception {
    15. System.out.println("来到了DisPatAction里面");
    16. //获取方法名
    17. String method = this.getMenthodName(req);
    18. System.out.println("请求的方法名==="+method);
    19. //获取类对象
    20. Class clszz = this.getClass();
    21. //调用方法
    22. Method method1 = clszz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
    23. //打开访问权限
    24. method1.setAccessible(true);
    25. //开始调用方法
    26. String invoke = (String)method1.invoke(this, req, resp);
    27. return invoke;
    28. }
    29. /**
    30. * 获取要操作的方法名
    31. * @param req req
    32. * @return 方法名
    33. */
    34. private String getMenthodName(HttpServletRequest req){
    35. //获取对象名
    36. String method = req.getParameter("methodName");
    37. //非空、如果方法名为空的话
    38. if(null==method||"".equals(method)){
    39. //获取map集合里面的方法名
    40. Map<String, String[]> parameterMap = req.getParameterMap();
    41. //获取值
    42. Set<String> strings = parameterMap.keySet();
    43. //遍历
    44. for(String key:strings){
    45. //判断键不为空
    46. if(-1!=key.indexOf("method:")){
    47. //开始替换
    48. method = key.replace("method:","");
    49. }
    50. }
    51. }
    52. return method;
    53. }
    54. }
    • ModelDriver.java 
    1. package com.jmh.framework;
    2. /**
    3. * 主要工作:给对象赋值
    4. */
    5. public interface ModelDriver<T> {
    6. public T getModel();
    7. }

     4.Action类(层)

    •  IndexAction.java
    1. package com.jmh.action;
    2. import com.jmh.entity.Index;
    3. import com.jmh.framework.Action;
    4. import com.jmh.framework.DisPatAction;
    5. import com.jmh.framework.ModelDriver;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. public class IndexAction extends DisPatAction implements ModelDriver<Index> {
    11. //实例化index
    12. private Index index=new Index();
    13. public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    14. System.out.println("来到了IndexAction里面");
    15. //设置编码
    16. req.setCharacterEncoding("utf-8");
    17. resp.setContentType("text/html;charset=utf-8");
    18. float sum=index.getSa()+index.getSb();
    19. //将获取到的数据保存到作用域
    20. req.getSession().setAttribute("sum",sum);
    21. return "failed";
    22. }
    23. public String delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    24. //设置编码
    25. req.setCharacterEncoding("utf-8");
    26. resp.setContentType("text/html;charset=utf-8");
    27. float sum=index.getSa()-index.getSb();
    28. //将获取到的数据保存到作用域
    29. req.getSession().setAttribute("sum",sum);
    30. return "failed";
    31. }
    32. public String chu(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    33. //设置编码
    34. req.setCharacterEncoding("utf-8");
    35. resp.setContentType("text/html;charset=utf-8");
    36. float sum=index.getSa()*index.getSb();
    37. //将获取到的数据保存到作用域
    38. req.getSession().setAttribute("sum",sum);
    39. return "failed";
    40. }
    41. public String cheng(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    42. //设置编码
    43. req.setCharacterEncoding("utf-8");
    44. resp.setContentType("text/html;charset=utf-8");
    45. float sum=index.getSa()/index.getSb();
    46. //将获取到的数据保存到作用域
    47. req.getSession().setAttribute("sum",sum);
    48. return "failed";
    49. }
    50. @Override
    51. public String toString() {
    52. return "IndexAction{}";
    53. }
    54. /**
    55. * 实现接口方法
    56. * @return
    57. */
    58. @Override
    59. public Index getModel() {
    60. return index;
    61. }
    62. }

     思路整理:第一步:我们需要编写一个中央控制器ActionServlet第二步:我们需要编写一个抽象类Action里面写一个抽象方法第三步:编写DisPatAction继承Action并重写抽象方法进行获取传来的方法名并进行反射实例化对象并根据获取的方法名进行反射调用指定的方法第四步:编写ModelDriver接口主要工作来给对象属性赋值

    第五步:编写IndexAction并继承DisPatAction实现ModelDriver主要来编写要操作的方法体业务逻辑代码

     2.自定义MVC模拟注册功能

    1.实体类

    • User.java

    1. package com.jmh.entity;
    2. import java.io.Serializable;
    3. public class User implements Serializable {
    4. private Integer id;//编号
    5. private String name;//姓名
    6. private String pwd;//密码
    7. private String sex;//性别
    8. private String age;//年龄
    9. private String hobby;//爱好
    10. public User(Integer id, String name, String pwd, String sex, String age, String hobby) {
    11. this.id = id;
    12. this.name = name;
    13. this.pwd = pwd;
    14. this.sex = sex;
    15. this.age = age;
    16. this.hobby = hobby;
    17. }
    18. public User() {
    19. }
    20. public String getPwd() {
    21. return pwd;
    22. }
    23. public Integer getId() {
    24. return id;
    25. }
    26. public String getName() {
    27. return name;
    28. }
    29. public String getSex() {
    30. return sex;
    31. }
    32. public String getAge() {
    33. return age;
    34. }
    35. public String getHobby() {
    36. return hobby;
    37. }
    38. public void setId(Integer id) {
    39. this.id = id;
    40. }
    41. public void setName(String name) {
    42. this.name = name;
    43. }
    44. public void setSex(String sex) {
    45. this.sex = sex;
    46. }
    47. public void setAge(String age) {
    48. this.age = age;
    49. }
    50. public void setHobby(String hobby) {
    51. this.hobby = hobby;
    52. }
    53. public void setPwd(String pwd) {
    54. this.pwd = pwd;
    55. }
    56. @Override
    57. public String toString() {
    58. return "User{" +
    59. "id=" + id +
    60. ", name='" + name + '\'' +
    61. ", pwd='" + pwd + '\'' +
    62. ", sex='" + sex + '\'' +
    63. ", age='" + age + '\'' +
    64. ", hobby='" + hobby + '\'' +
    65. '}';
    66. }
    67. }

     2.utils工具类

    •  ActionServlet.java
    1. package com.jmh.framework;
    2. import org.apache.commons.beanutils.BeanUtils;
    3. import org.dom4j.DocumentException;
    4. import javax.servlet.ServletConfig;
    5. import javax.servlet.ServletException;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.lang.reflect.InvocationTargetException;
    11. import java.lang.reflect.Method;
    12. import java.util.Enumeration;
    13. import java.util.HashMap;
    14. import java.util.Map;
    15. import java.util.Set;
    16. /**
    17. * 中央控制器
    18. */
    19. public class ActionServlet extends HttpServlet {
    20. //定义configModel对象
    21. private ConfigModel configModel;
    22. //定义默认配置文件.xml
    23. private String path="/config.xml";
    24. @Override
    25. public void init(ServletConfig config) throws ServletException{
    26. try {
    27. //获取配置文件key
    28. String path = config.getInitParameter("config");
    29. //判断key如果为空就赋默认值
    30. if(null==path){
    31. //初始化方法、读取配置文件
    32. configModel= ConfigModelFactory.bind(this.path);
    33. }else{
    34. //key里面有值
    35. configModel= ConfigModelFactory.bind(path);
    36. }
    37. } catch (DocumentException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. @Override
    42. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    43. doPost(req, resp);
    44. }
    45. @Override
    46. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    47. //设置编码
    48. req.setCharacterEncoding("utf-8");
    49. resp.setContentType("text/html;charset=utf-8");
    50. System.out.println("来到了中央控制器");
    51. //获取请求路径
    52. String servletPath = req.getServletPath();
    53. //获取路径最后一个点的下标
    54. int in = servletPath.lastIndexOf(".");
    55. //开始截取 从下标0到获取最后一个点的下标
    56. String path = servletPath.substring(0, in);
    57. try {
    58. //根据路径获取ActionModel对象
    59. ActionModel actionModel = this.createActionModel(path, configModel);
    60. //通过ActionModel对象获取全限定名实例化Action对象
    61. Action action = this.createAction(actionModel.getType());
    62. System.out.println("反射实例化==="+action);
    63. //开始调用给属性赋值的方法
    64. this.getModels(action,req);
    65. //调用方法
    66. String code = action.query(req, resp);
    67. //跳转是否转发、重定向方法
    68. this.reqresp(code,actionModel,req,resp);
    69. } catch (Exception e) {
    70. e.printStackTrace();
    71. }
    72. }
    73. /**
    74. * 根据.action路径获取ActionModel对象
    75. * @param path 路径
    76. * @param con configMOdel对象
    77. * @return ActionModel对象
    78. */
    79. private ActionModel createActionModel(String path,ConfigModel con){
    80. ActionModel action = con.pop(path);
    81. //非空
    82. if(null==action){
    83. throw new RuntimeException(path+"找不到");
    84. }
    85. //反之
    86. return action;
    87. }
    88. /**
    89. * 通过全限定名实例化Action对象
    90. * @param type 全限定名
    91. * @return
    92. * @throws ClassNotFoundException
    93. * @throws IllegalAccessException
    94. * @throws InstantiationException
    95. */
    96. private Action createAction(String type){
    97. try {
    98. if(null==type){
    99. throw new RuntimeException("传来的全限定名为空");
    100. }
    101. //获取类对象
    102. Class clazz = Class.forName(type);
    103. //通过类对象实例化对象
    104. Action action = (Action) clazz.newInstance();
    105. return action;
    106. }catch (Exception e){
    107. throw new RuntimeException(e);
    108. }
    109. }
    110. private void reqresp(String code,ActionModel a,HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException {
    111. //判断返回参数是否为空
    112. if(null==code||"".equals(code)){
    113. throw new RuntimeException("返回值为空");
    114. }
    115. //获取forward对象里面是否跳转路径
    116. ForwardModel forward = a.pop(code);
    117. //判断对象是否为空
    118. if(null==forward){
    119. throw new RuntimeException(code+"对应的对象找不到");
    120. }
    121. //判断是否跳转
    122. boolean redirect = forward.isRedirect();
    123. if(redirect){
    124. //为true重定向
    125. resp.sendRedirect(req.getContextPath()+forward.getPath());
    126. }else{
    127. //反之 转发
    128. req.getRequestDispatcher(forward.getPath()).forward(req,resp);
    129. }
    130. }
    131. /**
    132. * 给对象属性赋值
    133. */
    134. private void getModels(Action action,HttpServletRequest req) throws InvocationTargetException, IllegalAccessException {
    135. //判断对象是否实现了接口
    136. if(action instanceof ModelDriver){
    137. //实现了就强转
    138. ModelDriver model=(ModelDriver)action;
    139. //调用方法、给属性赋值
    140. Object model1 = model.getModel();
    141. //获取所有的请求参数
    142. Map<String, String[]> map = req.getParameterMap();
    143. Set<Map.Entry<String, String[]>> entries = map.entrySet();
    144. for(Map.Entry<String, String[]> str:entries){
    145. //System.out.println(str.getKey()+"《===》"+str.getValue());
    146. }
    147. BeanUtils.populate(model1,map);
    148. }
    149. }
    150. }
    •  Action.java(抽象类)
    1. package com.jmh.framework;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. /**
    7. * 子控制器的父类
    8. */
    9. public abstract class Action {
    10. //定义一个抽象方法
    11. public abstract String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, Exception;
    12. }
    •  DisPatAction.java
    1. package com.jmh.framework;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. import java.lang.reflect.Method;
    7. import java.util.Map;
    8. import java.util.Set;
    9. /**
    10. * 处理相识方法的功能
    11. */
    12. public class DisPatAction extends Action{
    13. @Override
    14. public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, Exception {
    15. System.out.println("来到了DisPatAction里面");
    16. //获取方法名
    17. String method = this.getMenthodName(req);
    18. System.out.println("请求的方法名==="+method);
    19. //获取类对象
    20. Class clszz = this.getClass();
    21. //调用方法
    22. Method method1 = clszz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
    23. //打开访问权限
    24. method1.setAccessible(true);
    25. //开始调用方法
    26. String invoke = (String)method1.invoke(this, req, resp);
    27. return invoke;
    28. }
    29. /**
    30. * 获取要操作的方法名
    31. * @param req req
    32. * @return 方法名
    33. */
    34. private String getMenthodName(HttpServletRequest req){
    35. //获取对象名
    36. String method = req.getParameter("methodName");
    37. //非空、如果方法名为空的话
    38. if(null==method||"".equals(method)){
    39. //获取map集合里面的方法名
    40. Map<String, String[]> parameterMap = req.getParameterMap();
    41. //获取值
    42. Set<String> strings = parameterMap.keySet();
    43. //遍历
    44. for(String key:strings){
    45. //判断键不为空
    46. if(-1!=key.indexOf("method:")){
    47. //开始替换
    48. method = key.replace("method:","");
    49. }
    50. }
    51. }
    52. return method;
    53. }
    54. }
    •  ModelDriver.java(接口)
    1. package com.jmh.framework;
    2. /**
    3. * 主要工作:给对象赋值
    4. */
    5. public interface ModelDriver<T> {
    6. public T getModel();
    7. }

     3.Action类

    • UserAction.java 
    1. package com.jmh.action;
    2. import com.jmh.entity.User;
    3. import com.jmh.framework.Action;
    4. import com.jmh.framework.DisPatAction;
    5. import com.jmh.framework.ModelDriver;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.io.PrintWriter;
    11. import java.lang.reflect.Array;
    12. import java.util.Arrays;
    13. import java.util.List;
    14. public class UserAction extends DisPatAction implements ModelDriver<User> {
    15. //实例化user对象
    16. private User user=new User();
    17. public String register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    18. //设置编码
    19. req.setCharacterEncoding("utf-8");
    20. resp.setContentType("text/html;charset=utf-8");
    21. //以防万一爱好不被覆盖就手动获取值
    22. String[] hobbies = req.getParameterValues("hobby");
    23. //获取到所有还好并遍历
    24. List<String> strings = Arrays.asList(hobbies);
    25. //遍历
    26. String hobbys="";
    27. for(String str:strings){
    28. hobbys+=str+",";
    29. }
    30. //截取从最后一个逗号的下标开始
    31. int i = hobbys.lastIndexOf(",");
    32. String substring = hobbys.substring(0, i);
    33. //赋值给对象的爱好
    34. user.setHobby(substring);
    35. //将对象保存到作用域里面
    36. req.getSession().setAttribute("sum",user);
    37. System.out.println("进来了==="+user);
    38. return "failed";
    39. }
    40. @Override
    41. public User getModel() {
    42. return user;
    43. }
    44. }
    这些代码如果不懂的话可以多看几遍或者自己赋值到编译器看看,或者自己手巧几遍熟练了就好,如果实在不懂或者想知道的话就可以评论,直接私我都ok
  • 相关阅读:
    一键集成prometheus监控微服务接口平均响应时长
    mac 竖屏显示屏鼠标无法从显示器移到mbp上
    解决maven骨架加载慢问题(亲测解决)
    第四课 标识符、关键字、变量、变量的分类和作用域、常量
    Django配置连接池:使用django-db-connection-pool配置连接池
    JavaWeb篇_08——Servlet技术以及第一个Servlet案例
    穿越时空的创新:解析云原生与Web3.0的奇妙渊源
    HTTP请求和响应(补充HTTP协议)
    变压器试验VR虚拟仿真操作培训提升受训者技能水平
    2022.8.8考试摄像师老马(photographer)题解
  • 原文地址:https://blog.csdn.net/m0_63300795/article/details/125607540