java培训技术RESTRUL_CRUD_添加操作

发布于:2022-12-06 ⋅ 阅读:(249) ⋅ 点赞:(0)

RESTRUL_CRUD_添加操作

  • 在jsp上增加连接

<a href=”empInput”>Add Employee</a>

  • 增加处理器方法

@RequestMapping(value=”/empInput”,method=RequestMethod.GET)

public String empInput(Map<String,Object> map){

map.put(“deptList”, departmentDao.getDepartments());

//解决错误:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute

Employee employee = new Employee();

//map.put(“command“, employee);

map.put(“employee“, employee);

return “add”;

}

  • 显示添加页面

<%@ page language=”java” contentType=”text/html; charset=UTF-8″

    pageEncoding=”UTF-8″ import=”java.util.*”%>

<%@ taglib prefix=”form” uri=”http://www.springframework.org/tags/form” %>

<!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>Insert title here</title>

</head>

<body>

<!–

1.为什么使用SpringMVC的form标签

① 快速开发

② 表单回显

2.可以通过modelAttribute指定绑定的模型属性,

若没有指定该属性,则默认从request域中查找command的表单的bean

如果该属性也不存在,那么,则会发生错误。

 –>

 <form:form action=”empAdd” method=”POST” modelAttribute=”employee”>

         LastName : <form:input path=”lastName”/><br><br>

         Email : <form:input path=”email”/><br><br>

         <%

                 Map<String,String> map = new HashMap<String,String>();

                 map.put(“1”, “Male”);

                 map.put(“0″,”Female”);

                 request.setAttribute(“genders”, map);

         %>

        Gender : <br><form:radiobuttons path=”gender” items=”${genders }” delimiter=”<br>”/><br><br>

         DeptName :

                 <form:select path=”department.id

                                                 items=”${deptList }”

                                                 itemLabel=”departmentName”

                                                 itemValue=”id”></form:select><br><br>

                 <input type=”submit” value=”Submit”><br><br>

 </form:form> 

</body>

</html>

  • 显示表单信息时,会报错:

想要了解跟多关于java培训课程内容欢迎关注尚硅谷java培训,尚硅谷除了这些技术文章外还有免费的高质量java培训课程视频供广大学员下载学习。

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到