Solution ideas and implementation steps for the problem of css and js becoming invalid after struts2 jump

Solution ideas and implementation steps for the problem of css and js becoming invalid after struts2 jump
When the jsp that is jumped to after the struts2 action is executed is displayed, the css does not work. Take the project where I am having problems as an example:
The actions are as follows:

Copy code
The code is as follows:

<action name="listUser" class="listUserAction">
<result>/users/userList.jsp</result>
</action>

( Note : The function of listUser is to find all users and then display them in userList.jsp under users. Since spring is used, the "listUserAction" here is the name configured in applicationContext.xml)
The css settings in userList.jsp are as follows :

Copy code
The code is as follows:

<link href="../css/style.css" rel="stylesheet" type="text/css" />

( Note : The directory hierarchy is like this, there are users and css folders under WebRoot, so if the jsp under users wants to call style.css, it needs to ../ go to the root directory and then find the css folder)
However, when the program jumps to /users/userList.jsp after successfully executing from listUser, /css/style.css does not work. If jsp is displayed directly in the address bar, it can be displayed normally.
The method found on the Internet says to use an absolute path for css :

Copy code
The code is as follows:

<link href="http://blog.163.com/<%=request.getContextPath%>/css/style.css" rel="stylesheet" type="text/css" />

But this will have an obvious disadvantage, which is that the portability will be poor.
Try to change result to <result>userList.jsp</result>, and then put jsp in the root directory, then there will be no such problem. At this time, the cause of the problem is almost known, that is, action is in the root directory. You can see it from the address bar when running the program, that is, the namespace is in the root directory, and then "../css/style.css" will naturally not be found.
Once you understand the problem, the solution will be easy to find. Find the package where listUser is configured and add namespace="/users". This is the time

Copy code
The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="users" extends="struts-default" namespace="/users">
......
<action name="listUser" class="listUserAction">
<result>/users/userList.jsp</result>
</action>
......
</package>
</strusts>

Then restart the server, OK, it displays normally! At the same time, we can also see that there is an additional "/users" before the action in the address bar, and when jumping to the connected video, it is also "/users/userList.jsp", which keeps it consistent and solves the problem.

<<:  Let's talk in detail about the direction of slow SQL optimization in MySQL

>>:  Detailed explanation of sample code for the improvement and changes brought by CSS variables to JS interactive component development

Recommend

Docker Basics

Preface: Docker is an open source application con...

Embed codes for several older players

The players we see on the web pages are nothing m...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

Some indicators of excellent web front-end design

The accessibility of web pages seems to be somethi...

Design Story: The Security Guard Who Can't Remember License Plates

<br />In order to manage the vehicles enteri...

vue+echarts realizes the flow effect of China map (detailed steps)

@vue+echarts realizes the flow effect of China ma...

Springboot+Vue-Cropper realizes the effect of avatar cutting and uploading

Use the Vue-Cropper component to upload avatars. ...

Nginx Linux installation and deployment detailed tutorial

1. Introduction to Nginx Nginx is a web server th...

The role and opening of MySQL slow query log

Preface The MySQL slow query log is a type of log...

MySQL 5.7.18 MSI Installation Graphics Tutorial

This article shares the MySQL 5.7.18 MSI installa...

Nginx+FastDFS to build an image server

Installation Environment Centos Environment Depen...

The easiest way to install MySQL 5.7.20 using yum in CentOS 7

The default database of CentOS7 is mariadb, but m...

The most common mistakes in HTML tag writing

We better start paying attention, because HTML Po...