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

Why is UTF-8 not recommended in MySQL?

I recently encountered a bug where I was trying t...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...

MySQL 8.0.20 installation and configuration tutorial under Win10

MySQL 8.0.20 installation and configuration super...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

Implementation of Docker container state conversion

A docker container state transition diagram Secon...

Common front-end JavaScript method encapsulation

Table of contents 1. Enter a value and return its...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...

Summary of 10 amazing tricks of Element-UI

Table of contents el-scrollbar scroll bar el-uplo...