[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

what is <html:base/> in Struts ?


i found meaning of   <html:base />  in Mastering jakarta Struts

<html:base />
============
The <html:base /> tag is used to insert an HTML <base> element, including an href pointing to the absolute
location of the hosting JSP page. This allows you to use relative URL references, rather than a URL that is
relative to the most recent requested resource. The <html:base /> tag has no body and supports a single
attribute target, which represents the target attribute of the HTML <base> tag. To use the <html:base /> tag,
you simply need to insert the tag, as shown in the following code snippet, at the top of your JSP:
<html:base/>




its confusing, not able to understand .  can anybody tell me with one example what this tag does ?

1) what happens if i use this tag and what happens if i DONT use this tag.
0
cofactor
Asked:
cofactor
  • 5
  • 4
1 Solution
 
bloodredsunCommented:
From the struts docs

"Renders an HTML <base> element with an href attribute pointing to the absolute location of the enclosing JSP page. This tag is valid only when nested inside an HTML <head> element.

This tag is useful because it allows you to use relative URL references in the page that are calculated based on the URL of the page itself, rather than the URL to which the most recent submit took place (which is where the browser would normally resolve relative references against)."

>>1) what happens if i use this tag and what happens if i DONT use this tag.

1./ Relative links will work from the page with the html:base

2./Not much. I've developed for quite some time on struts and have used it about twice:-)
0
 
bloodredsunCommented:
Where this really becomes useful is when you have a jsp that is found in one place, that is references by an action found at another

e.g.
JSP location
<webapp>/WEB-INF/pages/myPage.jsp

actions using myPage.jsp
<webapp>/myPage.do
<webapp>/extras/myOtherPage.do

This means that when you use the <html:base> tag in the jsp it outputs "/WEB-INF/pages" as the base url for this page which means you can use the same JSP in multiple actions in multiple locations. If the links where relative to the url, one of the two actions would have broken links for things like images if they were relative. With html:base, they don't.
0
 
cofactorAuthor Commented:
Hi, i also never usued this tag . but books always write this tag.

I am sorry, i could not grasp you.




>>/ Relative links will work from the page with the html:base

suppose i have a page  MyPage.jsp and this  MyPage.jsp  contains <html:base> tag .


folder structure :
=================

root/MyPage.jsp=====> suppose it contains <html:base>

root/MyFolder/MySecondPage.jsp

root/MyFolder/MySecondFolder/MyThirdPage.jsp


Now say, whats the power of this <html:base> ? what benefit it can give me ?


i made a demo example assuming u want to mean something like this. If u feel a better example plz provide that.

thank you
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cofactorAuthor Commented:
wow , u  r too fast .  let me read your post.
0
 
bloodredsunCommented:
Hi co,

here's a better example

simple.jsp in: <webapp>/WEB-INF/pages
-----------
<img src="../../images/myImage.gif"> <!-- goes to <webapp>/images to get the gif-->

without html:base

myaction.do (using simple.jsp) in: <webapp>/actions/myactions/subactions
-------------
<img src="../../images/myImage.gif"> <!-- goes to <webapp>/actions/images to get the gif, and fails-->

0
 
cofactorAuthor Commented:
i am sorry, i could not understand, though i tried to read line by line your statements. you wrote,


>JSP location
><webapp>/WEB-INF/pages/myPage.jsp

so mypage.jsp contains <html:base/>.OK ?  you are talking about actions . right ? well, so i am assuming there are two buttons in mypage.jsp . one button will call (if u press) http://localhost/myPage.do and other button will call (if u press ) http://localhost/extras/myOtherPage.do 










>which means you can use the same JSP in multiple actions in multiple locations.
>If the links >where relative to the url, one of the two actions would have broken links for >things like >images if they were relative. With html:base, they don't.

Sorry , no , i could not get you. here i am very much clueless. if u could give an example here abut the consequences probabily i could understand.

0
 
cofactorAuthor Commented:
once again u beat me !  well again let  me read carefully.
0
 
cofactorAuthor Commented:
i have  lost the morality to ask more question . because probabily you have tried to simplify it. unfortunately , its again out my bound. No, i could not  understand.

i am also unhappy to see the "pages" you kept in the WEB-INF folder (though legal ), but not good to see bcause JSP pages are kept in the top root dir(convention).

Anyway, it does not matter though. I know u are top expert in the forum. Unfortunately , it seems to me the most difficult thing in Struts . i am familiar with Struts but never usued <html:base/>. i will try to investigate your  statements  again  and will post my response tomorrow. thanks for efforts.
0
 
bloodredsunCommented:
>.am also unhappy to see the "pages" you kept in the WEB-INF folder (though legal ), but not good to see bcause JSP pages are kept in the top root dir(convention).

Actually no, in a Struts application, it's is considered best-prectise to put the jsps in the WEB-INF so that they cannot be accessed directly from the browser. It only means that you must put this path in the struts-config.xml

e.g. an example action-mapping using JSPs in the WEB-INF

<action path="/us_track" type="uk.co.brs.struts.actions.ActionTrack" name="trackerForm" validate="false" input="/WEB-INF/pages/us/us_track.jsp" scope="request">
    <forward name="reset" path="/WEB-INF/pages/us/us_track.jsp" />
    <forward name="success" path="/WEB-INF/pages/us/us_trackingresults.jsp" />
    <forward name="failure" path="/WEB-INF/pages/shared/error.jsp" />
</action>

If you leave the JSPs in the root directory, someone could access them directly e.g. <webapp>/myPage.jsp which would break as the JSP would be expecting to recieve an ActionForm to populate the page with data.

>> I know u are top expert in the forum.
While it is currently true, I'm not perfect and often make mistakes, so please feel free to ask/criticise me on anything..:-)

The best thing to do would be for you to have a play on a demo page making sure that your action and your jsp are at different depths in the directory structure
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now