Environment
Start page: webroot \ JSP \ login. jsp,
Jump to the target page: webroot \ JSP \ main. jsp
CSS path: webroot \ JSP \ CSS \ pda.css
CSS import of login. jsp:
<link href="css/pda.css" rel="stylesheet" type="text/css">
Form configuration in login. jsp:
<s:form action="loginAction.action" method="post">
Struts. xml configuration:
<Package name = "struts2" extends = "struts-Default"> <action name = "* Action" class = "useraction" method = "{1}"> <result name = "success">/main. JSP </result> <result name = "error">/login. JSP </result> </Action> <! -- JSP access --> <action name = "*"> <result>/JSP/{1}. jsp </result> </Action> </package> </struts>
Fault symptom:
When the target page is redirected to a specified page after processing in the action, the CSS style of the target page becomes invalid. the reason is that the dispatch jump Method of action changes the URL path of the page, leading to the failure of the relative path of the page to import CSS.
In this example, http: // localhost: 8080/xreb_wms_pda/login is used. action has accessed http: // localhost: 8080/xreb_wms_pda/JSP/login. JSP, submit the request to http: // localhost: 8080/xreb_wms_pda/loginaction on this page, and then jump to action. The page content is main. JSP content, but the path is still http: // localhost: 8080/xreb_wms_pda/loginaction, that is, the current page (main. JSP) the relative path is webroot, and the actual page should be under the/jsp directory, so according to the original CSS/pda.css
The file cannot be found, so an error occurs.
Solution:
1. Set the Redirect mode to redirect, which will cause data to be transmitted using session
<action name="*Action" class="userAction" method="{1}"><result name="success" type="redirect">/main.jsp</result><result name="error">/login.jsp</result></action>
2. Change the http: // localhost: 8080/xreb_wms_pda/loginaction path to http: // localhost: 8080/xreb_wms_pda/JSP/loginaction
Step 1:
<Package name = "struts2" extends = "struts-Default" namespace = "/jsp" <action name = "* Action" class = "useraction" method = "{1 }"> <result name = "success">/main. JSP </result> <result name = "error">/login. JSP </result> </Action> <! -- JSP access --> <action name = "*"> <result>/JSP/{1}. jsp </result> </Action> </package> </struts>
Step 2:
<s:form action="loginAction.action" method="post" theme="simple" namespace="/jsp">
Solve the problem!
Conclusion: When a JSP or other page file accesses a resource file, the time is when it is compiled and displayed. At this time, it is critical that the page path can locate the CSS file according to the encoding path, therefore, you need to consider the page path after the jump.