代码实现
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private LogService logService;
/**
* 事务管理员
* @param outName : 出账人
* @param inName : 入账人
* @param money : 金额
* @throws Exception
*/
//@Transactional(readOnly = false)
public void transfer(String outName, String inName, Double money) throws Exception {
logService.addLog("UserServiceImpl transfer");
//出账
userDao.outMoney(outName, money);
//System.out.println(1 / 0);
//入账
userDao.inMoney(inName, money);
}
}
@Service
public class LogServiceImpl implements LogService {
@Autowired
private LogDao logDao;
/**
* 事务协调员
*
* @param content
* @throws Exception
*/
@Transactional(propagation = Propagation.MANDATORY)
public void addLog(String content) throws Exception {
logDao.addLog(content, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//System.out.println(1 / 0);
}
}
本质 :将dao的实现子类放到spring容器中
开发步骤
①引入依赖
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jclartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.16version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.7version>
dependency>
dependencies>
②定义dao接口及其实现子类
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SqlSessionFactory sqlSessionFactory;//由Spring容器传入
public int addUser(User inputUser) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
int addUser = sqlSession.update("addUser", inputUser);
sqlSession.commit();
sqlSession.close();
return addUser;
}
public List<User> selectUserList() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("selectUserList");
sqlSession.close();
return userList;
}
}
③编写spring.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.panghu">context:component-scan>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="mybatis-config.xml">property>
bean>
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}">property>
<property name="url" value="${jdbcUrl}">property>
<property name="username" value="${user}">property>
<property name="password" value="${password}">property>
bean>
beans>
④编写mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.panghu.pojo"/>
typeAliases>
<mappers>
<mapper resource="com/panghu/dao/UserDao.xml"/>
mappers>
configuration>
⑤编写UserDao.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.panghu.dao.UserDao">
<select id="selectUserList" resultType="com.panghu.pojo.User">
select * from t_user
select>
<insert id="addUser">
insert into t_user values (null, #{userName},#{userPwd},#{money},#{address});
insert>
mapper>
⑥代码测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void addUser() {
}
@Test
public void selectUserList() throws Exception {
List<User> userList = userDao.selectUserList();
System.out.println("userList = " + userList);
}
}
代码实现
@Repository
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
//调用SqlSessionDaoSupport的setSqlSessionFactory方法
super.setSqlSessionFactory(sqlSessionFactory);
}
public int addUser(User inputUser) throws Exception {
int addUser = getSqlSession().update("addUser", inputUser);
return addUser;
}
public List<User> selectUserList() throws Exception {
List<User> userList = getSqlSession().selectList("selectUserList");
return userList;
}
}
代码实现
public class BaseDao extends SqlSessionDaoSupport {
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
//调用SqlSessionDaoSupport的setSqlSessionFactory方法
super.setSqlSessionFactory(sqlSessionFactory);
}
}
@Repository
public class UserDaoImpl extends BaseDao implements UserDao {
public int addUser(User inputUser) throws Exception {
int addUser = getSqlSession().update("addUser", inputUser);
return addUser;
}
public List<User> selectUserList() throws Exception {
List<User> userList = getSqlSession().selectList("selectUserList");
return userList;
}
}
开发步骤
①引入依赖
②定义dao接口
③编写spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.panghu.dao.UserDao">property>
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="mybatis-config.xml">property>
bean>
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}">property>
<property name="url" value="${jdbcUrl}">property>
<property name="username" value="${user}">property>
<property name="password" value="${password}">property>
bean>
beans>
④编写mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.panghu.pojo"/>
typeAliases>
configuration>
⑤编写UserDao.xml
⑥代码测试
代码实现
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.panghu.dao">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="mybatis-config.xml">property>
bean>
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}">property>
<property name="url" value="${jdbcUrl}">property>
<property name="username" value="${user}">property>
<property name="password" value="${password}">property>
bean>
beans>
需求
代码实现
@WebServlet("/selectUserList")
public class UserController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = applicationContext.getBean(UserService.class);
try {
List<User> userList = userService.selectUserList();
System.out.println("userList = " + userList);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
存在问题
概述
代码实现
public class MyContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//项目启动,创建ApplicationContext对象,并将其存放到ServletContext域对象中
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
sce.getServletContext().setAttribute("applicationContext", applicationContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//项目关闭
}
}
@WebServlet("/selectUserList")
public class UserController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute("applicationContext");
UserService userService = applicationContext.getBean(UserService.class);
try {
List<User> userList = userService.selectUserList();
System.out.println("userList = " + userList);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
存在问题
概述
代码实现
<web-app>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
context-param>
<listener>
<listener-class>com.panghu.listener.MyContextLoaderListenerlistener-class>
listener>
web-app>
/**
* SpringMVC团队写(oldqiu)
*/
public class ApplicationContextConstant {
public static final String APPLICATIONCONTEXT_NAME = "applicationContext";
}
/**
* SpringMVC团队写(oldqiu)
*/
public class MyApplicationContextUtils {
public static ApplicationContext getApplicationContext(ServletContext servletContext){
ApplicationContext applicationContext = (ApplicationContext) servletContext
.getAttribute(ApplicationContextConstant.APPLICATIONCONTEXT_NAME);
return applicationContext;
}
}
/**
* SpringMVC团队写(oldqiu)
*/
public class MyContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//项目启动,创建ApplicationContext对象,并将其存放到ServletContext域对象中
//获取全局初始化参数contextConfigLocation
String contextConfigLocation = (String) sce.getServletContext().getInitParameter("contextConfigLocation");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation);
sce.getServletContext().setAttribute(ApplicationContextConstant.APPLICATIONCONTEXT_NAME, applicationContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//项目关闭
}
}
/**
* 普通开发者(李冠鱼)
*/
@WebServlet("/selectUserList")
public class UserController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext applicationContext = MyApplicationContextUtils.getApplicationContext(getServletContext());
UserService userService = applicationContext.getBean(UserService.class);
try {
List<User> userList = userService.selectUserList();
System.out.println("userList = " + userList);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
存在问题
代码实现
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>com.panghu.controller.base.MyDispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
public class MyDispatcherServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String requestURI = req.getRequestURI();
String[] paths = requestURI.split("/");
System.out.println("paths = " + paths);
//获取对象名称,根据对象名称获取对应的bean
String beanId = paths[2];
ApplicationContext applicationContext = MyApplicationContextUtils.getApplicationContext(getServletContext());
Object instance = applicationContext.getBean(beanId);
//获取方法名称,执行对应的方法
String methodName = paths[3];
try {
Method method = instance.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(instance,req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
@Controller("userController")
public class UserController {
@Autowired
private UserService userService;
public void selectUserList(HttpServletRequest request , HttpServletResponse response) throws Exception {
List<User> userList = userService.selectUserList();
System.out.println("userList = " + userList);
}
public void addUser(HttpServletRequest request , HttpServletResponse response) throws Exception {
userService.addUser(new User(null,"aaaa","aaaa",10000.0,"武汉市"));
}
}
开发步骤
①引入依赖
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<junit.version>4.13.2junit.version>
<lombok.version>1.18.22lombok.version>
<spring.version>5.3.13spring.version>
<logback.version>1.2.7logback.version>
<slf4j.version>1.7.32slf4j.version>
<servlet.version>4.0.1servlet.version>
<thymeleaf.version>3.0.12.RELEASEthymeleaf.version>
properties>
<dependencies>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>${thymeleaf.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>${servlet.version}version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>${logback.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
dependencies>
②编写web.xml,配置DispatcherServlet
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
③编写spring-mvc.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.panghu">context:component-scan>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
bean>
property>
bean>
property>
bean>
beans>
④定义UserController
@Controller
public class UserController {
@RequestMapping("/selectUserList")
public ModelAndView selectUserList(){
ModelAndView modelAndView = new ModelAndView();
List<User> userList = new ArrayList<>();
userList.add(new User(1,"张三","12345",10000.0,"码头"));
userList.add(new User(2,"李四","12345",20000.0,"魔都"));
userList.add(new User(3,"王五","12345",30000.0,"帝都"));
userList.add(new User(4,"赵六","12345",40000.0,"NewYork"));
userList.add(new User(5,"田七","12345",40000.0,"WashTon"));
//相当于放到request域对象
modelAndView.addObject("userList",userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
}
⑤编写user-list.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表title>
head>
<body>
<ul>
<li th:each="user : ${userList}" th:text="${user.userName}">li>
ul>
body>
html>
核心组件配置
代码实现1
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean>
代码实现2(推荐)
<mvc:annotation-driven>mvc:annotation-driven>
概述
代码实现1
<mvc:resources mapping="/css/**" location="/css/">mvc:resources>
<mvc:resources mapping="/html/**" location="/html/">mvc:resources>
<mvc:resources mapping="/img/**" location="/img/">mvc:resources>
<mvc:resources mapping="/js/**" location="/js/">mvc:resources>
代码实现2(推荐)
<mvc:default-servlet-handler>mvc:default-servlet-handler>
源码
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};//设置访问路径
@AliasFor("value")
String[] path() default {};//设置访问路径
RequestMethod[] method() default {};//设置请求方式
String[] params() default {};//设置请求参数
String[] headers() default {};//设置请求头
}
作用
代码实现
/**
* 20-@RequestMapping注解之匹配方式
* ①精确匹配
* @return
*/
@RequestMapping("/request/test1")
public ModelAndView test1(){
System.out.println("RequestController test1");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
/**
* ②模糊匹配
* @return
*/
@RequestMapping("/request/test2/*")
public ModelAndView test2(){
System.out.println("RequestController test2");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
代码实现
@Controller
@RequestMapping("/request")
public class RequestController {
/**
* 20-@RequestMapping注解之匹配方式
* ①精确匹配
* @return
*/
@RequestMapping("/test1")
public ModelAndView test1(){
System.out.println("RequestController test1");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
/**
* ②模糊匹配
* @return
*/
@RequestMapping("/test2/*")
public ModelAndView test2(){
System.out.println("RequestController test2");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
代码实现
//@RequestMapping(value = "/request/test3" ,method = RequestMethod.GET)
@GetMapping("/request/test3")
public ModelAndView test3(){
System.out.println("RequestController test3");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
@RequestMapping(value = "/demo03.html" )
public ModelAndView toDemo03Page(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("demo03");
return modelAndView;
}
代码实现
@RequestMapping(value = "/request/test4" ,params = {"username","password"})
public ModelAndView test4(){
System.out.println("RequestController test4");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
需求
代码实现
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>25-请求参数绑定之简单类型title>
head>
<body>
<form th:action="@{/request/test5}" method="get">
账户:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<button type="submit">提交button>
form>
body>
html>
@RequestMapping("/request/test5")
public ModelAndView test5(String username , String password){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
注意事项
源码
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";//等价于name
@AliasFor("value")
String name() default "";//设置请求参数名称
boolean required() default true;//设置请求参数是否必须
String defaultValue() default ValueConstants.DEFAULT_NONE;//设置请求参数默认值
}
代码实现
@RequestMapping("/request/test6")
public ModelAndView test6(
@RequestParam(name = "username" ,required = false ,defaultValue = "missing") String myUserName ,
@RequestParam(name = "password",required = false,defaultValue = "missing") String myPassword){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
代码实现
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>27-请求参数绑定之实体类</title>
</head>
<body>
<form th:action="@{/request/test7}" method="get">
账户:<input type="text" name="userName"><br>
密码:<input type="text" name="userPwd"><br>
<button type="submit">提交</button>
</form>
</body>
</html>
@RequestMapping("/demo07.html")
public ModelAndView toDemo07Page(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("demo07");
return modelAndView;
}
@RequestMapping("/request/test7")
public ModelAndView test7(User inputUser){
System.out.println("inputUser = " + inputUser);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
注意事项