对于<welcome-file-list>设置主页后无法引用CSS的问题

本文介绍了在Eclipse中开发Web项目时如何正确配置非WebRoot目录下的主页及CSS路径,包括使用JSP和HTML的不同处理方式。

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

在eclipse中对web程序进行开发,当把非webroot根目录下的文件设置为主页时,如果对css目录采用相对路径,将会无法应用,这时需要对文件的相对路径进行修改,如果是jsp文件,直接加头文件对目录进行设置:

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


在原来的css目录中加上<%=basePath%>


而如果是html,则复制过来变成jsp,记得把头文件改成<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>。否则会乱码。

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssm</display-name> <!-- 编码过滤器开始 --> <filter> <filter-name>charset</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> </filter> <filter-mapping> <filter-name>charset</filter-name> <!-- <url-pattern>*.action</url-pattern> --> <servlet-name>springmvc</servlet-name> </filter-mapping> <!-- 编码过滤器结束 --> <!-- 配置spring容器的监听器 开始 目地:在启用tomcat的时候加载 applicationContext.xml ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml') --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置上下文的参数 contextConfigLocation 在org.springframework.web.context.ContextLoader里面--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置spring容器的监听器 结束--> <!-- 配置前端控制器开始 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置contextConfigLocation --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- load-on-startup 1 代表当tomcat启动加web.xml里就创建 DispatcherServlet的对象 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- *.action:所以的在xxxx.action结尾的请求都交给DispatcherServlet /* 所以的请求都交给DispacherServlet 包含静态文件的地址 js css png gif,使用此种方式可以实现 RESTful风格的url --> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- 配置前端控制器结束 --> <!--欢迎页面 当tomcat启动的时候 直接执行的页面--> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> classpath填什么?
最新发布
08-08
<?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_3_1.xsd" version="3.1"> <!-- 1. 字符编码过滤器(必须放在第一个filter) --> <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> <!-- 2. 静态资源处理过滤器(新增) --> <filter> <filter-name>staticResourceFilter</filter-name> <filter-class>org.springframework.web.filter.ResourceUrlEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>staticResourceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 3. Spring MVC 前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring-mvc.xml</param-value> </init-param> <init-param> <param-name>throwExceptionIfNoHandlerFound</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 4. 静态资源放行配置(关键修改) --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/statics/*</url-pattern> <url-pattern>/images/*</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.jpg</url-pattern> <url-pattern>*.gif</url-pattern> <url-pattern>*.ico</url-pattern> <url-pattern>*.woff</url-pattern> <url-pattern>*.woff2</url-pattern> <url-pattern>*.ttf</url-pattern> <url-pattern>*.svg</url-pattern> </servlet-mapping> <!-- 5. Spring MVC 映射(必须放在静态资源之后) --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 6. 错误页面配置(可选) --> <error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location> </error-page> <!-- 7. 欢迎页配置 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>/这是web.xml界面/<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>流浪狗救助站 - 首页</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/css/style.css" /> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { // 动态数字动画效果 $('.stat-number').each(function() { var $this = $(this); var target = parseInt($this.text().replace('+', '')); var current = 0; var increment = target / 50; // 控制动画速度 var timer = setInterval(function() { current += increment; if (current >= target) { clearInterval(timer); current = target; $this.text(target + '+'); } else { $this.text(Math.floor(current) + '+'); } }, 20); }); // 平滑滚动到锚点 $('a[href^="#"]').on('click', function(event) { event.preventDefault(); $('html, body').animate({ scrollTop: $($.attr(this, 'href')).offset().top }, 800); }); }); </script> </head> <body> <header class="navbar"> <div class="nav-container"> <ul class="nav-menu"> <li class="active"><a href="${pageContext.request.contextPath}/views/user/home.jsp">首页</a></li> <li><a href="${pageContext.request.contextPath}/dogs">待领养狗狗</a></li> <li><a href="${pageContext.request.contextPath}/views/user/about.jsp">关于我们</a></li> <li><a href="${pageContext.request.contextPath}/views/user/contact.jsp">联系我们</a></li> <c:if test="${not empty sessionScope.user}"> <li><a href="${pageContext.request.contextPath}/logout">退出登录</a></li> </c:if> </ul> </div> </header> <div class="hero"> <h1 class="hero-title">为流浪狗发声,让世界更美好</h1> <p class="hero-subtitle">携手保护流浪动物,共建和谐生态环境</p> <div class="btn-container"> <a href="${pageContext.request.contextPath}/views/login.jsp" class="btn btn-primary">立即行动</a> <a href="#about-section" class="btn btn-secondary">了解更多</a> </div> </div> <section class="stats"> <div class="stat-item"> <div class="stat-number">25+</div> <div class="stat-label">救助项目</div> </div> <div class="stat-item"> <div class="stat-number">1200+</div> <div class="stat-label">志愿者</div> </div> <div class="stat-item"> <div class="stat-number">50+</div> <div class="stat-label">合作伙伴</div> </div> </section> <!-- 新增的详细介绍部分 --> <section id="about-section" class="about-section"> <div class="container"> <h2>关于潦草小狗救助站</h2> <div class="about-content"> <div class="about-text"> <h3>我们的使命</h3> <p>潦草小狗救助站成立于2010年,是一个非营利性组织,致力于为无家可归的流浪狗提供庇护、医疗和寻找永久家庭的机会。我们相信每只狗都值得被关爱和保护。</p> <h3>我们的工作</h3> <p>我们的工作包括:</p> <ul> <li>救助街头流浪狗并提供临时庇护</li> <li>为狗狗提供必要的医疗护理和绝育服务</li> <li>通过领养计划为狗狗寻找永久家庭</li> <li>开展公众教育活动,提高动物保护意识</li> <li>倡导动物福利政策的改进</li> </ul> <h3>我们的成就</h3> <p>自成立以来,我们已经:</p> <ul> <li>救助了超过5000只流浪狗</li> <li>为3000多只狗狗找到了永远的家</li> <li>开展了200多场公众教育活动</li> <li>建立了覆盖全市的志愿者网络</li> </ul> </div> <div class="about-image"> <img src="${pageContext.request.contextPath}/images/rescue-dog.jpg" alt="被救助的狗狗"> </div> </div> </div> </section> <section class="how-to-help"> <div class="container"> <h2>如何帮助它们</h2> <div class="help-options"> <div class="help-option"> <div class="help-icon">🏠</div> <h3>领养</h3> <p>给无家可归的狗狗一个温暖的家。领养不仅拯救了一只狗的生命,也为其他需要救助的狗狗腾出了空间。</p> <a href="${pageContext.request.contextPath}/dogs" class="btn btn-small">查看待领养狗狗</a> </div> <div class="help-option"> <div class="help-icon">🤝</div> <h3>志愿者</h3> <p>加入我们的志愿者团队,参与狗狗的日常照顾、遛狗、清洁等工作。您的每一分钟都能带来改变。</p> <a href="${pageContext.request.contextPath}/views/user/contact.jsp" class="btn btn-small">成为志愿者</a> </div> <div class="help-option"> <div class="help-icon">💝</div> <h3>捐赠</h3> <p>您的捐款将直接用于狗狗的食物、医疗和庇护所运营。每一分钱都能帮助改善狗狗的生活,助力它们走向更好的未来。</p> <a href="#" class="btn btn-small">立即捐赠</a> </div> </div> </div> </section> </body> </html>/这是index.jsp,为什么css文件不体现
07-11
有网页的源码,怎么在idea里用vue复现,运用element-UI组件,view-source:https://rloopbase.nju.edu.cn/ ,源码如下: <html xmlns="http://www.w3.org/1999/xhtml " xml:lang="en" lang="en"> <link rel="stylesheet" type="text/css" href="/css/myIndex.css"> <head> <title>R-loopBase</title> <link rel="icon" href="/images/icon-rloop-purple-circle-2.png" type="image/x-icon"> </head> <body> <div class="container"> <div class="indexBar"></div> <div class="card"> <!-- Introduction --> <div class="cardTitle"> Introduction </div> <div class="cardContext"> <p id="introductionText" class="cardP">The R-loop, a three-stranded nucleic acid structure composed of an RNA:DNA hybrid and a displaced single-stranded DNA, plays versatile roles in many physiological and pathological processes. However, its controversial genomic localization and incomplete understanding of its regulatory network raise great challenges for R-loop research. Here, we present R-loopBase to tackle these pressing issues by systematic integration of a huge amount of genomics and literature data. A reference set of human R-loop zones is computed with confidence scores and detailed annotations (<span style="font-weight: bold"><a href="/index.jsp">Search</a></span>, <span style="font-weight: bold"><a href="/help.jsp">Help</a></span> and <span style="font-weight: bold"><a href="/download.jsp">Download</a></span>), all of which can be visualized in well-annotated genomic context in a local genome browser (<span style="font-weight: bold"><a href="https://rloopbase.nju.edu.cn/browser/cgi-bin/hgTracks ">Browser</a></span>). A list of 1,185 R-loop regulators is manually curated from PubMed literatures and annotated with most up-to-date multi-omics data (<span style="font-weight: bold"><a href="/rloopRegulator.jsp">Regulator</a></span></span>). We have also manually curated 24 R-loop regulators in mouse, 63 in yeast (<i>saccharomyces cerevisiae</i>) and 21 in <i>Escherichia coli</i> to facilicate R-loop research in these model organisms (<span style="font-weight: bold"><a href="/rloopRegulator.jsp">Regulator</a></span>). We further deduce the functional relationship between individual R-loops and their putative regulators (<span style="font-weight: bold"><a href="/rloopRegulator.jsp">Regulator</a></span> and <span style="font-weight: bold"><a href="/download.jsp">Download</a></span>). In total, we have generated billions of entries of functional annotations, which can be easily accessed in our user-friendly interfaces (<span style="font-weight: bold"><a href="/help.jsp">Help</a></span>). The regulator summary and genomic annotation of R-loop regions will be constantly updated along the research progress of field. You are welcome to contribute to updating R-loop regulators and related literatures (Regulator Updating System on <span style="font-weight: bold"><a href="/rloopRegulator.jsp">Regulator</a></span> page). Any suggestions and feedbacks from you are also welcome (<span style="font-weight: bold"><a href="/contact.jsp">Contact</a></span>). </p> </div> <a href="##"><div id="showIntroduction" onclick="showMoreOrLess()" style="text-decoration: underline; text-align: right; padding-right: 20px; margin-top: -15px;">Show more</div></a> <hr class="cardHr"> </div> <!-- Introduction --> <div class="searchContainer"> <div class="searchLogo"> <ul> <li class="searchLogoText">R-loopBase</li> <li class="searchLogoImg"><img src="/images/icon-rloop-purple-circle-2.png" style="height: 60px;"></li> </ul> </div> <div class="searchForm"> <input id="searchText" type="text" name="" placeholder="e.g. BRCA2 or chr1:154572702-154628593" onkeypress="handle(event)"> <a href="#"><div class="searchItem" href="#" onclick="toGeneList()"> <i>search</i> </div></a> </div> </div> <div class="horizontalMenu"> <!-- horizontal menu --> <ul id="indexNavigator"> <li> <a href="/browser/cgi-bin/hgTracks"> <img src="/images/browser.png"> <div>Browser</div> </a> </li> <li> <a href="/rloopRegulator.jsp"> <img src="/images/regulator.png"> <div>Regulator</div> </a> </li> <li> <a href="https://rloopbase.nju.edu.cn/deepr/tool/model "> <img src="/images/tool.png"> <div>Tool</div> </a> </li> <li> <a href="/download.jsp"> <img src="/images/download.png"> <div>Download</div> </a> </li> <li> <a href="/help.jsp"> <img src="/images/help.png"> <div>Help</div> </a> </li> <li> <a href="/contact.jsp"> <img src="/images/contact.png"> <div>Contact</div> </a> </li> </ul> </div> <!-- horizontal menu --> <div class="twoCardContainer"> <div class="card cardLeft"><!-- News --> <div class="cardTitle"> News </div> <div class="cardContext" id="newsDiv" style="overflow:hidden;clear:both;height:220px"> <ul> <li>2023.10.01 Online of <a href='https://rloopbase.nju.edu.cn/deepr/tool/model '>DeepER</a>, an R-loop prediction tool.</li> <li>2022.02.19 A new <a href="https://doi.org/10.1101/2022.02.18.480986 ">preprint</a> by R-loopBase team.</li> <li>2021.11.18 Published on <a href="https://doi.org/10.1093/nar/gkab1103 ">Nucleic Acids Res</a>.</li> <li>2021.06.20 Official release of R-loopBase v1.0.</li> <li>2021.06.15 Internal evaluation before public release.</li> <li>2021.05.10 Build-up of R-loopBase genome browser.</li> <li>2021.03.31 Data freeze for R-loopBase v1.0.</li> <li>2021.01.18 Launching R-loopBase project.</li> <li>2020.10.10 Systematic evalation of all R-loop mapping technologies.</li> </ul> </div> <a href="##"><div id="showNews" onclick="showMoreOrLess2()" style="text-decoration: underline; text-align: right; padding-right: 20px; margin-top: 4px;">Show more</div></a> <hr class="cardHr"> </div><!-- News --> <div class="card cardRight"> <!-- Publication --> <div class="cardTitle"> Latest Publications </div> <div class="cardContext" id="publicationDiv"> <ul> <li><a style="color: black" href="https://pubmed.ncbi.nlm.nih.gov/38590928/ ">LUC7L3 is a downstream factor of SRSF1 and prevents genomic instability.</a> Cell Insight. 2024.</li> <li><a style="color: black" href="https://pubmed.ncbi.nlm.nih.gov/38677845/ ">Smartphone-based device for rapid and single-step detection of piRNA-651 using anti-DNA:RNA hybrid antibody and enzymatic signal amplification.</a> Anal Chim Acta. 2024.</li> <li><a style="color: black" href="https://pubmed.ncbi.nlm.nih.gov/38609257/ ">Split activator of CRISPR/Cas12a for direct and sensitive detection of microRNA.</a> Anal Chim Acta. 2024.</li> </ul> <div style="width: 100%; text-align: right; margin-top: -4px;"><a href="https://pubmed.ncbi.nlm.nih.gov/?term=%28R-loop%29+OR+%28R-loops%29+OR+%28%22RNA+DNA+hybrid%22%29+OR+%28%22RNA+DNA+hybrids%22%29+OR+%28%22DNA+RNA+hybrid%22%29+OR+%28%22DNA+RNA+hybrids%22%29&sort=pubdate ">More publications in PubMed</a></div> </div> <hr class="cardHr"> </div> <!-- Publication --> </div> <div class="footer"> <div style="background-color: rgb(90,19,92); height: 60px; line-height: 60px; text-align: center; color: white; "> <div style="display: inline-block; height: 20px; line-height: 20px; margin-top: 15px; margin-bottom: 4px;">© 2020-2025 NANJING UNIVERSITY. ALL RIGHTS RESERVED. </div> <div style="height: 20px; line-height: 20px; margin-top: 0px;">CONDITIONS OF USE</div> </div> </div> </div> <!-- container --> </body> </html> <script type="text/javascript" language="JavaScript"> function handle(e){ if(e.keyCode === 13){ e.preventDefault(); // Ensure it is only this code that runs toGeneList(); } } function toGeneList(){ //var type = document.getElementById('searchSelect').value; var type; var value = document.getElementById('searchText').value; if(value==''){ alert("Please input keyword!"); }else if(value.match(/[^a-zA-Z0-9:-]/)){ alert("Only numbers, letters, \":\" and \"-\" are allowed!"); }else{ //top.location = '/geneList.jsp?searchFor=' + type + '&keyword=' + value; if(value.match(/^chr.+:\d+-\d+$/)){ type="location"; }else{ type="symbols"; } top.location = '/rloopList.jsp?searchFor=' + type + '&keyword=' + value; } } function changeLiSize(){ var selectId = document.getElementById("myNavigator"); } function showMoreOrLess(){ var selectId = document.getElementById("showIntroduction"); var selectText = document.getElementById("introductionText"); if(selectId.innerText == "Show more"){ selectText.style.height="auto"; selectId.innerText="Show less"; }else{ selectText.style.height="120px"; selectId.innerText="Show more"; } } function showMoreOrLess2(){ var selectId2 = document.getElementById("showNews"); var selectText2 = document.getElementById("newsDiv"); if(selectId2.innerText == "Show more"){ selectText2.style.height="auto"; selectId2.innerText="Show less"; }else{ selectText2.style.height="220px"; selectId2.innerText="Show more"; } } var publicationDivHeight = document.getElementById('publicationDiv').clientHeight; </script>
07-29
这是我的表结构: 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、付费专栏及课程。

余额充值