1.创建用户的实体类代码如下:
package com.txw.domain; import lombok.Data; import lombok.ToString; import java.io.Serializable; /** * 用户实体类 * @author Adair */ @Data // 自动生成set和get方法 @ToString // 重写toString方法 @SuppressWarnings("all") // 注解警告信息 public class User implements Serializable { private String userName; // 用户名 private String passWord; // 密码 private Integer age; // 年龄 }2.创建返回响应控制器类的代码如下:
package com.txw.controller; import com.txw.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * 返回响应控制器类 * @author Adair */ @Controller @RequestMapping(value = "/user") @SuppressWarnings("all") // 注解警告信息 public class UserController { /** * 返回字符串 * @param model * @return */ @RequestMapping(path = "/testString") public String testString(Model model){ System.out.println("testString方法执行了..."); // 模拟从数据库中查询出User对象 User user = new User(); user.setUserName("美美"); user.setPassWord("123"); user.setAge(30); // model对象 model.addAttribute("user",user); return "success"; } }3.在response.jsp编写如下代码:
<%-- Created by IntelliJ IDEA. User: Adair Date: 2020/7/2 0002 Time: 10:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>返回响应</title> </head> <body> <a href="user/testString" >testString</a> </body> </html>4.修改success.jsp的代码如下:
<%-- Created by IntelliJ IDEA. User: Adair Date: 2020/7/1 0001 Time: 18:17 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>成功页面</title> </head> <body> <h3>执行成功!!!!!!</h3> ${user.userName} ${user.passWord} ${user.age} </body> </html>5.配置Tomcat的界面如图所示: 6.使用TomCat运行结果如图: 7.通过浏览器访问http://localhost:8080/response.jsp结果如图所示: 8.点击testString会跳转到如图所示的界面: 9.控制台打印结果如图所示:
