ResourceBundleMessageSource resolving error - java

I have a JSP-Tagfile which renders the html-header and defines my Javascript/Stylesheet resources.
<%# tag language="java" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<spring:theme code="main.css"/>"/>
<script type="text/javascript" src="<spring:theme code="default.js"/>"></script>
...
</html>
Now i wan't to load a i18n messages for the javascript stuff.
var button_ok='<spring:message code="js.button.ok" javaScriptEscape="true"/>';
When i use this inside the tag-file it works as supposed and resolves the messages, even if there is only a default messages.properties (fallback).
But if i load the messages with an external javascript file lang.js.jsp it only tries to resolve the message code against the properties for the current language and the fallback to the default one is not working.

If the message should be resolved inside the tag, the ApplicationContext defined within the DispatcherServlet is used. Otherwise the ApplicationContext from the ContextLoaderListener is used (root application context).
To solve the problem i moved the ResourceBundleMessageSource into the configuration which is loaded by the ContextLoaderListener.

Related

What is the difference between <html> and <html:html> element in JSP?

I am developing a simple Struts 1.x web application and there's a file named success.jsp and this is the sample code:
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%# taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success.jsp</title>
<html:base/>
</head>
<body>
Go to myStart
</body>
</html:html>
By default, <html>was used instead of <html:html>, may I know what is the major difference between these two elements? Is it necessary to specify the uses of them? Besides, what is the major function for <html:base/> element?
Btw I found some definitions for these elements but I need clarification:
<html:html> Renders an HTML <html> element with language attributes extracted from the user's current Locale object, if there is one.
<html:base> Renders an HTML 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).
The <html:html> tag is a Struts 1.x JSP Taglib directive, declared in this line on your JSP Page:
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
These custom tag(s) are typically of the form <prefix:tagname>. The prefix declared on taglib is what binds your taglib container to the list of markups available in the taglib.
In essence <html:html> tells the taglib, prefixed html to render a html element when JSP is rendered.
So to answer your question <html> is a HTML directive while <html:html> is a Struts JSP taglib tag to generate a HTML <html> directive.

Custom tag libraries in JSP with Java EE 7

I am having a difficult time to find the right way to implement my custom tags in JSP with Java EE 7. I could see the web.xml 'taglig' elements have been obsoleted and Java EE 7 documentation talks just about JSF custom tags implementation but not JSPs.Unfortunately, I don't have a chance to move to JSF. Any help is greatly appreciated.
You might need to be more specific on your custom tag use case.
The easiest way of defining a custom tag is defining .tag file (type of JSP with extension .tag) and place it into {yourapp}/WEB-INF/tags/{dir}
e.g.
I have a custom.tag in WEB-INF/tags/custom
<%#attribute name="greetName" type="java.lang.String"%>
<div>Hello, ${greetName }</div>
In my index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib tagdir="/WEB-INF/tags/custom" prefix="ctm" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP in EE7</title>
</head>
<body>
<div>Before tag</div>
<ctm:custom greetName="John"></ctm:custom>
<div>After tag</div>
</body>
</html>
Output:
There's also way to define tag using java code:
1. Create a .tld file in WEB-INF
2. Extending BodyTagSupport or SimpleTagSupport class
.tag file is great for reusing some fragment of html and extend the *TagSupport classes if you need more flexibility.

Struts 2, multiple views and jQuery - How do I reuse common code?

I've read most of the online resources for building a simple "Hello World" app using Java and Struts 2. I understand the simple stuff. My problem is that I'm trying to expand that learning and build a large scale app, and I just don't see how to connect the dots.
Scenario:
I've got three views to begin with: Home.jsp, MyAccount.jsp, Contact.jsp. Each has the following HTML:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
...
</head>
<body>
<!-- If logged in, says "hello <s:property name="username">"
Else displays link to .show() #loginPane -->
<div id="accountHeader">...</div>
<!-- Displays <s:textfield> tags and <s:submit> tag to accept username and password -->
<div id="loginPane" style="display: none">...</div>
<header>...</header>
<nav>...</nav>
<!-- Displays view-specific content that includes Struts 2 tags -->
<div id="content">...</div>
<footer>...</footer>
</body>
</html>
So, obviously there is a lot of code common to each view (anything not in #content div).
How do I architect these views for code reuse?
What I've tried:
Placing common code in common.js and using jQuery .html() calls to populate <div>s. [Doesn't work because jQuery cannot generate code with <s:> tags.]
Using only one .jsp view file and placing view-specific code in common.js to be generated with jQuery .html() calls. [Doesn't work for the same reason -- jQuery cannot generate code with <s:> tags.]
Placing all view components in .jspf files and loading each with jQuery .load() calls from common.js. [Doesn't work -- I'm guessing the .jspf files need the Struts 2 <%taglib ...%> included in each, but jQuery .load() treats the <%taglib ...%> as text to be displayed in the <div>... and also fails to properly generate the <s:> tags.]
What is the proper way to do this? How do I architect my view(s) for code reuse?
My apologies if this isn't the proper forum to ask for architecture help, but I'm really struggling here... Perhaps point me to a more appropriate forum or an online tutorial that addresses this type of architecture?
Thanks in advance!
I've used several methods to accomplish this type of re-use of code including Tiles and tooling around with Sitemesh and other template frameworks. What I've found is that, much as Steven Benitez, in the end I preferred to use JSP taglibs, native Struts2 taglibs, and JSTL to essentially build out my own templating routines. The main reason I prefer this is that there tends to be less overhead and it's been a lot easier to maintain and extend in the long run.
Generally What I do is define my base template, index.jsp for example, and then in each independent Struts controller class I will define what page fragment is used. I try to split my controllers up in such a way that each page or function is handled by a single controller and I implement the Preparable interface. This way I can set a parameter for the page to reference. Sometimes I set it as a variable in the controller class, sometimes a sessions variable depending on what type of stating I need for the application.
Once I have a variable with the page to reference, I can just use a JSTL import or Struts include tag to load the page fragment.
The controller class would look something like this:
#Results({
#Result(name = "success", location = "/WEB-INF/content/index.jsp")
})
public class IndexController extends RestActionSupport implements Preparable{
private String page;
private String pageTitle;
#Override
public void prepare() throws Exception {
page = "home";
pageTitle= "My Home Page";
}
...
}
And then the JSP would look something like this:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title> ${pageTitle}</title>
</head>
<body>
<c:import url="${page}.jsp" />
</body>
</html>
EDIT: Fragment page example:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="s" uri="/struts-tags"%>
<div>
<h1>Welcome Home!</h1>
</div>
<script type='text/javascript'>
$(document).ready(function() {
// page specific scripting if needed
);
</script>
You can encapsulate common template code in JSP tag files, as explained in this answer or you can also use decorator frameworks such as Tiles or SiteMesh to do this.
Personally, I prefer JSP tag files. Do not attempt to write out HTML with jQuery or to put all of your code into a single JSP.

Spring variables not carrying into view

I'm very new to Spring and can't seem to figure out what's wrong here. I'm sure it has to be something simple, but anyway:
I was going through the tutorial on this page, and I have nearly identical code. After pulling down the Apache Commons library and the JSTL stuff, I was in business. Everything works, down to it actually using the jsp I specified as the view, but the variable "message" in the controller is not being displayed when the site is rendered. It's not because of the expression language stuff, either, because I'm not getting the ${message} text displaying either. It's just a blank page.
The only reason I know that the jsp is actually being triggered is because I put a title in there that is no where else and it is being used on page display. I also know that the variable is being set in the controller and the action is being called because of a simple sysout that I put in the mapped function.
Thanks for any help!
EDIT
Here's the jsp:
<%#page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>om nom JSP Page</title>
</head>
<body>
${message}
</body>
</html>
And the controller:
package org.me.home.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class SomeController {
#RequestMapping("/somepage")
public ModelAndView someAction() {
String mymsg = "Saying hi!";
System.out.println(mymsg);
return new ModelAndView("somepage", "message", mymsg);
}
}
Try to use such a construction:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${message}" />
And are you sure, that the problem is in displaying? Is page loaded fine, I mean is all other HTML displayed on page, besides "message"?
EDIT
Try to use org.springframework.web.servlet.ModelAndView instead of
org.springframework.web.portlet.ModelAndView
Your tomcat maybe using the wrong jstl.jar. Under your tomcat lib folder, check if your jstl.jar is the same version you are using with your project.

How to have dynamic css files depending on struts session variable

I have a webpage in which i want the css file to be the same name as a session variable I have set.
For example;
If the session variable was "blue", i want the page to load the CSS file blue.css.
I tried something below which didnt work, and I'm now stuck. My knowledge of struts is very limited.
<LINK rel="stylesheet" type="text/css"
href="<html:rewrite page='/css/<c:out value="${brand}"/>.css'/>">
This is the full code listing at the top of my jsp page
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html-el" prefix="html-el"%>
<html:html lang="true">
<head>
<LINK rel="stylesheet" type="text/css" href="<html:rewrite page='/css/${brand}.css'/>">
<html:base/>
I do not know how to find the version of JSP and JSTL I am using. This was a project picked up from someone else and I have never used them before
It isn't working because you put the c:out tag inside the attribute of the html:rewrite tag. Struts tags don't parse when within attribute values.
href="<html:rewrite page="/css/${brand}.css"/>">
according to another user you cant have html rewrite in the attribute but maybe you can do something like below and rewrite the whole link?
<html:rewrite page='<LINK rel="stylesheet" type="text/css" href="/css/${brand}.css"/>'>
Unfortunatily, I couldnt not get any of your answers working. I think it may be a problem with my versions clashing.
For now I have managed to do it by doing;
<%
String brand = (String) session.getAttribute("brand");
if (brand == null || brand.equals("")) {
skin = "standard";
}
%>
<link href="/css/<%=brand%>.css" rel="stylesheet" type="text/css" />
Try this java script, It will work.
variable code is a hidden field in ur jsp.
var code=(document.getElementById('code')).value;
var url;
// Dynamically loads the Style Sheet according to the user logged in
if(code !== null)
{
url="<link rel='stylesheet' type='text/css' href='css/"+code+".css' title='default'>";
}
else
{
url="<link rel='stylesheet' type='text/css' href='css/commonStyle.css' title='default'>";
}
if(url !== null)
{
document.write(url);
}

Categories