Failure of CSS and JS after struts2 redirection (Path Problems after redirection)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.