[转载]jsp中pageEncoding、charset=utf8"、(request/response).setCharacterEncoding("utf8")

本文详细解析了JSP/Servlet中pageEncoding、contentType、request.setCharacterEncoding及response.setCharacterEncoding的作用,阐述了如何通过合理设置这些参数避免中文乱码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在JSP/Servlet 中主要有以下几个地方可以设置编码,pageEncoding="UTF-8"、contentType="text/html;charset=UTF -8"、request.setCharacterEncoding("UTF-8")和response.setCharacterEncoding ("UTF-8"),其中前两个只能用于JSP中,而后两个可以用于JSP和Servlet中。

1、pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码。

       众所周知,JSP在服务器上是要先被编译成Servlet的。pageEncoding="UTF-8"的作用就是告诉JSP编译器在将JSP文件编译成Servlet时使用的编码。通常,在JSP内部定义的字符串(直接在JSP中定义,而不是从浏览器提交的数据)出现乱码时,很多都是由于该参数设置错误引起的。例如,你的JSP文件是以GBK为编码保存的,而在JSP中却指定pageEncoding="UTF-8",就会引起JSP内部定义的字符串为乱码。

       另外,该参数还有一个功能,就是在JSP中不指定contentType参数,也不使用response.setCharacterEncoding方法时,指定对服务器响应进行重新编码的编码。

2、contentType="text/html;charset=UTF-8"的作用是指定对服务器响应进行重新编码的编码。

       在不使用response.setCharacterEncoding方法时,用该参数指定对服务器响应进行重新编码的编码。服务器在将数据发送到浏览器前,对数据进行重新编码时,使用的就是该编码。

3、request.setCharacterEncoding("UTF-8")的作用是设置对客户端请求进行重新编码的编码。

       该方法用来指定对浏览器发送来的数据进行重新编码(或者称为解码)时,使用的编码。

4、response.setCharacterEncoding("UTF-8")的作用是指定对服务器响应进行重新编码的编码。

       服务器在将数据发送到浏览器前,对数据进行重新编码时,使用的就是该编码。

       其次,要说一说浏览器是怎么样对接收和发送的数据进行编码的

       response.setCharacterEncoding("UTF-8")的作用是指定对服务器响应进行重新编码的编码。同时,浏览器也是根据这个参数来对其接收到的数据进行重新编码(或者称为解码)。所以在无论你在JSP中设置response.setCharacterEncoding ("UTF-8")或者response.setCharacterEncoding("GBK"),浏览器均能正确显示中文(前提是你发送到浏览器的数据编码是正确的,比如正确设置了pageEncoding参数等)。读者可以做个实验,在JSP中设置 response.setCharacterEncoding("UTF-8"),在IE中显示该页面时,在IE的菜单中选择"查看(V)"à"编码 (D)"中可以查看到是" Unicode(UTF-8)",而在在JSP中设置response.setCharacterEncoding ("GBK"),在IE中显示该页面时,在IE的菜单中选择"查看(V)"à"编码(D)"中可以查看到是"简体中文(GB2312)"。

       浏览器在发送数据时,对URL和参数会进行URL编码,对参数中的中文,浏览器也是使用response.setCharacterEncoding参数来进行URL编码的。以百度和GOOGLE为例,如果你在百度中搜索"汉字",百度会将其编码为"%BA%BA%D7%D6"。而在GOOGLE中搜索 "汉字",GOOGLE会将其编码为"%E6%B1%89%E5%AD%97",这是因为百度的 response.setCharacterEncoding参数为GBK,而GOOGLE的的 response.setCharacterEncoding参数为UTF-8。

       浏览器在接收服务器数据和发送数据到服务器时所使用的编码是相同的,默认情况下均为JSP页面的response.setCharacterEncoding参数(或者contentType和 pageEncoding参数),我们称其为浏览器编码。当然,在IE中可以修改浏览器编码(在IE的菜单中选择"查看(V)"à"编码(D)"中修改),但通常情况下,修改该参数会使原本正确的页面中出现乱码。一个有趣的例子是,在IE中浏览GOOGLE的主页时,将浏览器编码修改为"简体中文(GB2312)",此时,页面上的中文会变成乱码,不理它,在文本框中输入"汉字",提交,GOOGLE会将其编码为"%BA%BA%D7%D6",可见,浏览器在对中文进行URL编码时,使用的就是浏览器编码。

       弄清了浏览器是在接收和发送数据时,是如何对数据进行编码的了,我们再来看看服务器是在接收和发送数据时,是如何对数据进行编码的。

       对于发送数据,服务器按照response.setCharacterEncoding—contentType—pageEncoding的优先顺序,对要发送的数据进行编码。

       对于接收数据,要分三种情况。一种是浏览器直接用URL提交的数据,另外两种是用表单的GET和POST方式提交的数据。

       因为各种WEB服务器对这三种方式的处理也不相同,所以我们以Tomcat5.0为例。

       无论使用那种方式提交,如果参数中包含中文,浏览器都会使用当前浏览器编码对其进行URL编码。

       对于表单中POST方式提交的数据,只要在接收数据的JSP中正确设置request.setCharacterEncoding参数,即将对客户端请求进行重新编码的编码设置成浏览器编码,就可以保证得到的参数编码正确。有写读者可能会问,那如何得到浏览器编码呢?上面我们提过了,在默认请情况下,浏览器编码就是你在响应该请求的JSP页面中response.setCharacterEncoding设置的值。所以对于POST表单提交的数据,在获得数据的JSP页面中request.setCharacterEncoding要和生成提交该表单的JSP页面的 response.setCharacterEncoding设置成相同的值。

       对于URL提交的数据和表单中GET方式提交的数据,在接收数据的JSP中设置request.setCharacterEncoding参数是不行的,因为在Tomcat5.0中,默认情况下使用ISO- 8859-1对URL提交的数据和表单中GET方式提交的数据进行重新编码(解码),而不使用该参数对URL提交的数据和表单中GET方式提交的数据进行重新编码(解码)。要解决该问题,应该在Tomcat的配置文件的Connector标签中设置useBodyEncodingForURI或者 URIEncoding属性,其中useBodyEncodingForURI参数表示是否用request.setCharacterEncoding 参数对URL提交的数据和表单中GET方式提交的数据进行重新编码,在默认情况下,该参数为false(Tomcat4.0中该参数默认为true); URIEncoding参数指定对所有GET方式请求(包括URL提交的数据和表单中GET方式提交的数据)进行统一的重新编码(解码)的编码。 URIEncoding和useBodyEncodingForURI区别是,URIEncoding是对所有GET方式的请求的数据进行统一的重新编码(解码),而useBodyEncodingForURI则是根据响应该请求的页面的request.setCharacterEncoding参数对数据进行的重新编码(解码),不同的页面可以有不同的重新编码(解码)的编码。所以对于URL提交的数据和表单中GET方式提交的数据,可以修改 URIEncoding参数为浏览器编码或者修改useBodyEncodingForURI为true,并且在获得数据的JSP页面中 request.setCharacterEncoding参数设置成浏览器编码。

下面总结下,以Tomcat5.0为WEB服务器时,如何防止中文乱码。

1.对于同一个应用,最好统一编码,推荐为UTF-8,当然GBK也可以。

2.正确设置JSP的pageEncoding参数

3.在所有的JSP/Servlet中设置contentType="text/html;charset=UTF-8"或response.setCharacterEncoding("UTF-8"),从而间接实现对浏览器编码的设置。

4.对于请求,可以使用过滤器或者在每个JSP/Servlet中设置request.setCharacterEncoding("UTF-8")。同时,要修改Tomcat的默认配置,推荐将useBodyEncodingForURI参数设置为true,也可以将URIEncoding参数设置为 UTF-8(有可能影响其他应用,所以不推荐)。

这是我的表结构: DROP TABLE IF EXISTS `animalphotos`; CREATE TABLE `animalphotos` ( `photo_id` int NOT NULL AUTO_INCREMENT COMMENT ' 照片ID', `animal_id` int NULL DEFAULT NULL COMMENT ' 所属动物ID', `photo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图片路径或URL', PRIMARY KEY (`photo_id`) USING BTREE, INDEX `animal_id`(`animal_id` ASC) USING BTREE, CONSTRAINT `animalphotos_ibfk_1` FOREIGN KEY (`animal_id`) REFERENCES `animals` (`animal_id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for animals -- ---------------------------- DROP TABLE IF EXISTS `animals`; CREATE TABLE `animals` ( `animal_id` int NOT NULL AUTO_INCREMENT COMMENT '动物唯一标识', `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '名称', `species` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ' 种类', `gender` enum('雄','雌') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT ' 性别', `age` int NULL DEFAULT NULL COMMENT ' 年龄', `user_id` int NULL DEFAULT NULL COMMENT ' 关联饲养者/管理者(Users.user_id)', `zone` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '动物位置', PRIMARY KEY (`animal_id`) USING BTREE, INDEX `caretaker_id`(`user_id` ASC) USING BTREE, INDEX `species_id`(`species` ASC) USING BTREE, CONSTRAINT `animals_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for animaltraits -- ---------------------------- DROP TABLE IF EXISTS `animaltraits`; CREATE TABLE `animaltraits` ( `trait_id` int NOT NULL AUTO_INCREMENT COMMENT ' 特征ID', `animal_id` int NULL DEFAULT NULL COMMENT '动物ID(Animals)', `nature` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '性格 ', `favourite` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT ' 喜好描述', PRIMARY KEY (`trait_id`) USING BTREE, INDEX `animal_id`(`animal_id` ASC) USING BTREE, CONSTRAINT `animaltraits_ibfk_1` FOREIGN KEY (`animal_id`) REFERENCES `animals` (`animal_id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for users -- ---------------------------- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `user_id` int NOT NULL AUTO_INCREMENT COMMENT ' 用户唯一标识', `username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名', `password` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码(加密)', `role` enum('游客','饲养者','管理员') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT ' 角色:游客/饲养者/管理员', `status` enum('在线','离线') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户状态(是否在线)', PRIMARY KEY (`user_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1; 这是我的实体类: // User 类 public class User { private int user_id; private String username; private String password; private String role; private String status; private List<Animals> animals; // 用户与动物是一对多关系 // getter 和 setter 方法 } // Animals 类 public class Animals { private int animal_id; private String name; private String gender; // 雄/雌 private Integer age; private String species; private String zone; private User user; // 动物与用户是一对多关系(反向关联) private List<AnimalPhoto> animalPhotos; // 动物与照片是一对多关系 // getter 和 setter 方法 } // AnimalPhoto 类 public class AnimalPhoto { private int photo_id; private Animals animal; // 照片与动物是一对多关系(反向关联) private String photo_url; // getter 和 setter 方法 } // AnimalTrait 类 public class AnimalTrait { private int trait_id; private Animals animal; // 特征与动物是一对一关系(反向关联) private String nature; // 性格 private String favourite; // 喜好描述 // getter 和 setter 方法 } 用户 (users) 与动物 (animals) 之间是一对多关系。 动物 (animals) 与照片 (animalphotos) 之间是一对多关系。 动物 (animals) 与特征 (animaltraits) 之间是一对一关系。 这是我登录注册代码: package com.cxy.Controller; import com.cxy.Pojo.User; import com.cxy.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; import java.text.SimpleDateFormat; import java.util.Date; @Controller public class UserController { @Autowired private UserService userService; // 显示登录页面 @GetMapping("/login") public String loginPage(@RequestParam(value = "error", required = false) String error, Model model) { if (error != null) { model.addAttribute("error", true); } return "/login/login.jsp"; // 返回登录页面 } // 处理登录请求 @PostMapping("/login") public String login(@RequestParam String username, @RequestParam String password, HttpSession session, Model model) { User user = userService.login(username, password); if (user != null) { // 更新数据库中的在线状态 userService.updateUserStatus(user.getUserId(), "在线"); // 将用户信息存入 session session.setAttribute("user", user); session.setAttribute("username", user.getUsername()); // 存储用户名 session.setAttribute("role", user.getRole()); // 存储角色 session.setAttribute("status", "在线"); // 存储状态 session.setAttribute("loginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); // 登录时间 return "redirect:/index.jsp"; // 登录成功,跳转到主页 } // 登录失败,返回登录页面并传递错误信息 model.addAttribute("loginError", "用户名或密码错误,请重新尝试。"); return "login/login.jsp"; } // 显示注册页面 @GetMapping("/register") public String registerPage() { return "/login/register.jsp"; // 返回注册页面 } // 处理注册请求 @PostMapping("/register") public String register(User user, Model model) { if (userService.register(user)) { return "/login/login.jsp"; // 注册成功,跳转到登录页面 } model.addAttribute("registerError", "用户名已存在"); // 用户名已存在 return "/login/register.jsp"; // 注册失败,返回注册页面 } // 处理退出登录 @GetMapping("/logout") public String logout(HttpSession session) { User user = (User) session.getAttribute("user"); if (user != null) { userService.updateUserStatus(user.getUserId(), "离线"); } session.invalidate(); // 清除session return "redirect:/login/login.jsp"; // 返回登录页面 } } 这是我实现登录注册的配置: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cxy.Mapper.UserMapper"> <!-- 查询用户 --> <select id="findByUsername" resultType="com.cxy.Pojo.User"> SELECT * FROM users WHERE username = #{username} </select> <!-- 插入新用户 --> <insert id="insertUser" parameterType="com.cxy.Pojo.User"> INSERT INTO users (username, password, role) VALUES (#{username}, #{password}, #{role}) </insert> <!-- 更新在线状态 --> <update id="updateStatus"> UPDATE users SET status = #{status} WHERE user_id = #{user_id} </update> </mapper> application-dao.xml:<?xml version="1.0" encoding="UTF-8"?> <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 http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cxy.Mapper"/> </bean> </beans> application-service.xml:<?xml version="1.0" encoding="UTF-8"?> <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 http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="com.cxy.Service"/> </beans> spring-mvc.xml:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.cxy.Controller"/> <mvc:annotation-driven/> <mvc:resources mapping="/css/**" location="/css/" /> </beans> web.xml:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring DispatcherServlet --> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置 Spring DispatcherServlet 映射 --> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> index.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <title>动物园数字档案管理系统</title> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/index.css"> </head> <body> <!-- 顶部导航栏 --> <header> <div class="logo"> <img src="<%= request.getContextPath() %>/images/zoo-logo.png" alt="Zoo Logo"> </div> <nav> <ul> <li><a href="${pageContext.request.contextPath}/index.jsp">首页</a></li> <li><a href="${pageContext.request.contextPath}/animal.jsp">动物信息</a></li> <li><a href="${pageContext.request.contextPath}/activities.jsp">活动预约</a></li> <li><a href="${pageContext.request.contextPath}/forum.jsp">用户交流</a></li> <% String username = (String) session.getAttribute("username"); String userRole = (String) session.getAttribute("role"); String userStatus = (String) session.getAttribute("status"); if ("饲养者".equals(userRole)) { out.println("<li><a href='" + request.getContextPath() + "/animal/list'>我的动物</a></li>"); } else if ("管理员".equals(userRole)) { out.println("<li><a href='" + request.getContextPath() + "/animal/list'>所有动物</a></li>"); } if (username == null) { out.println("<li><a href='" + request.getContextPath() + "/login/login.jsp'>登录/注册</a></li>"); } else { out.println("<li><a href='" + request.getContextPath() + "/logout'>退出登录</a></li>"); out.println("<li><span>登录时间: " + session.getAttribute("loginTime") + "</span></li>"); } %> </ul> </nav> </header> <!-- 主要内容 --> <div class="content"> <% if ("游客".equals(userRole)) { %> <div class="animal-search"> <h2>探索动物世界</h2> <input type="text" placeholder="搜索动物..."> <button onclick="window.location.href='${pageContext.request.contextPath}/activities.jsp'">立即预约活动</button> </div> <% } else if ("饲养者".equals(userRole)) { %> <div class="manage-animals"> <h2>饲养者面板</h2> <p>欢迎,<%= username %>!您当前状态:<%= userStatus %></p> </div> <% } else if ("管理员".equals(userRole)) { %> <div class="admin-panel"> <h2>管理员面板</h2> <p>欢迎,<%= username %>!您当前状态:<%= userStatus %></p> </div> <% } else { %> <div class="welcome-message"> <h2>欢迎来到动物园数字档案管理系统</h2> <p>请登录或注册以获取更多功能。</p> <button onclick="window.location.href='${pageContext.request.contextPath}/login/login.jsp'">登录/注册</button> </div> <% } %> </div> <!-- 底部信息栏 --> <footer> <div class="footer-content"> <p>©2023 动物园数字档案管理系统</p> <p>联系我们:info@zoodatabase.com</p> </div> </footer> </body> </html> add_animal.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <title>添加动物 - 动物园数字档案管理系统</title> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/animal_form.css"> </head> <body> <jsp:include page="/header.jsp"/> <div class="container"> <h1>添加新动物</h1> <form action="${pageContext.request.contextPath}/animal/add" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="name">动物名称:</label> <input type="text" id="name" name="name" required> </div> <div class="form-group"> <label for="species">种类:</label> <input type="text" id="species" name="species" required> </div> <div class="form-group"> <label for="gender">性别:</label> <select id="gender" name="gender" required> <option value="">请选择</option> <option value="雄">雄</option> <option value="雌">雌</option> </select> </div> <div class="form-group"> <label for="age">年龄:</label> <input type="number" id="age" name="age" min="0" required> </div> <div class="form-group"> <label for="zone">位置:</label> <input type="text" id="zone" name="zone" required> </div> <div class="form-group"> <label for="photo_url">上传照片:</label> <input type="file" id="photo_url" name="photo_url" multiple> </div> <div class="form-group"> <label for="nature">性格:</label> <input type="text" id="nature" name="nature" required> </div> <div class="form-group"> <label for="favourite">喜好描述:</label> <textarea id="favourite" name="favourite"></textarea> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">添加动物</button> <a href="${pageContext.request.contextPath}/animal/list" class="btn btn-secondary">取消</a> </div> </form> </div> 注意:查询查的是动物完整的信息(包括图片),添加也要添加动物完整的信息,可以对数据库插入动物图片。这需要对表进行关联。 请根据我的配置和我给出的添加动物页面add_aniaml.jsp,用javaee的ssm整合框架实现饲养者和管理者对动物的增删改查,,以及写出对应的jsp页面:在/animal/目录内,并修改index页面。包括(controller接口,mapper接口,service,serviceimpl和Mapper新配置)。其中饲养者登录时:显示我的动物导航栏,点击进入页面只能对自己添加的动物进行修改数据和删除,也可以在该页面添加新的动物。而管理者登录时:显示所有动物导航栏,点击进入页面,可以对全部动物信息增删改。饲养者和管理者可以对动物所有信息进行写入。
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值