Spring MVC, how to include another controller/view inside a view (partialview) - java

I'm just starting out with Spring MVC having been trying to pickup Java(Web) for the few months.
I'm sure what I'm trying to achieve is very simply, but I can't find a solution that really works.
I'd like to include another controller/view inside a view. This could be a menu, or some other dynamically generated content. For example, let's say in a side bar of an application I need to display a list of categories. I don't want to have to retrieve these categories in every controller/view that needs to use them. I'd like to include another controller/view into the main page view so that this content can be placed in multiple views but managed by a single piece of code.
ASP.NET MVC has something called PartialViews, and most PHP frameworks seem to offer this kind of functionality, but I can't find anything like this in spring.
The closest I've come to resolving this issue is to use the jsp:include tag, which does work, infact it's almost the solution, but it generates errors in Eclipse because obviously Eclipse can't locate the path, as it's a spring RequestMapping i.e. /include/categories rather than a direct link to a physical file.
The other solution is to use Javascript to dynamically load content into the sidebar, but I don't want to do that.
Is there a 'correct' way to do this, or is there a way to supress the errors generated by eclipse for the jsp:include tag?

If the content your are including is static and you don't have a lot of different pages, your approach with including jsps is ok. Otherewise, look at the templating frameworks like Tiles or Thymeleaf.

Related

How to reuse a JSP with a controller?

I'm new to web development with Java, so please excuse me if I'm butchering the terminology:
I'm building a web app with JSPs/servlets using the Java MVC model. I'm including a register/login option on the top menu that will of course need to communicate with the server (handle registering/logging in or retreiving the user's name).
I want to reuse both the JSP and controller code for the top menu as it should be on every page. I'm able to reuse the menu page using <c:import>. However, the menu will appear on pages that have their own functionality and therefore their own controllers. I can't figure out how to reuse the controller code for the menu on these pages as I can only map one servlet to a URL.
I don't have much code to show as an example at this point. What's the best practice for reusing common functionality like this without interfering with page specific functionality?
Not sure if I understand the problem correctly.
What about passing a hidden field in your form(which, from my understanding would point to the same mapping in your controller) specifying which jsp the request is coming from ? That way you know which jsp to call back from your controller.

Can Jodd provide functionality like apache tiles framework?

I am using jodd in my project. I am using lots of functionality jodd like madvoc , petite. I am looking functionality like apache tiles framework I have seen and read the post http://jodd.org/doc/decora/ but I don't get to much. Can any one provide working example of decora.
Ok, this is not short question - so please be a bit more concrete here on StackOverflow :)
Decora is quite simple and somewhat different then Apache Tiles. It reminds on old, good SiteMesh. Now, let me explain how it works:
with your business logic, create an design-free html page. Generate the content right in the <body>.
Now, create a template with design, colors, images... Choose where you want your content to be injected; like inside certain div.
After you apply Decora, the body content from the page will be decorated with the template, by inserting it into this div.
This is how it works in a nutshell. You have working example in Uphea but just note that is a bit older version of Jodd used. Also, there is quickstart project, that has Decora configured.
Decora is more powerful then above - you may have more regions on one page, set the header tags like title and so on. You may use html tags for marking the regions, or JSP tags, etc.

How to render plain html in Play 2.0

I'm working on learning JSP and the Play framework, and I understand that it runs on Scala and renders views based on templates, but what if I just want to use plain HTML rather than scala templates?
The situation I'm in is that I'm designing the site to match a visual template, so I'm using Dreamweaver to build the html files. I really like Play framework though, so I'd like to continue using it. So, what are my options here?
I don't get. Play's views are not just nice html files, of course you can (or even should) use your favorite tools for design part, anyway you have to also learn how to include a dynamic parts in it.
Of course you can use DreamWeaver for that task as it has feature for editing source code. But I can ensure you from my own experience, that there are better tools for every-day work with Play's views than DW.
You can also use plain HTML in your /public folder however in this scenario you won't be able to make it dynamic, so it has no sense, as you can create the pages without any framework - just using static files created with DW.
In general words: you need to verify your needs, cause from your question I read: "I like Play framework, anyway I don't want to use it for its job..."
After-comments edit:
You don't have to make views dynamic. If you won't pass any arguments into the view and will put there pure HTML it will be 'relatively cheap' way for displaying static pages as well. Just you need to remeber to leave first line of the file empty. So you don't need to use File index = new File... instead just put your bare HTML code into ie: app/views/staticContact.scala.html and then use an action:
public static Result staticContact(){
return ok(views.html.staticContact.render());
}
On the quite other hand, last time I was wondering if it wasn't better to put HTML code of the static pages into the DB, in such case you could create an editing page, where you could change HTML without redeploying the application. All what you will need it was just fetching HTML from DB and displaying it in one generic view. For better performance you can use included Cache implementation.
GET / controllers.Assets.at(path="/public/html", file="index.html")
This is working for play 2.0.1 for /public/html/index.html file

How do I translate a page in java with lot of text,without using resource bundle?

I have a web page with lots of text.Is there any means through which I can translate it,without using resource bundle(which involves using properties files,requiring key value pairs for all words.)?
Thanks for your precious time.
An alternative is to create separate views for each language. So a "mypage_en_US.html" for the US-english version and a "mypage_en_GB.html" for the british-english version. This gives you total control over the text and layout but has the drawback of possible code duplication if there is any logic in your view.
Wicket uses pretty clean views which should hardly contain any logic so this works pretty well there.
Just be innovative here. If you are getting shitty copy pase work. Write a program to convert the properties file and then use that properties file using google translate api, but yeah end of the day you will have to go with properties file.
I belive there would be other way too using google translate api again, would love to hear that myself too
Depends on your web framework.
For example, Wicket can apply I18N on webpages in two ways :
- using I18N files and resourcesbundles, with placeholders where required in the page
- by having totally separate pages, one for each language. The page template itself is postfixed with the locale, much like property files : HomePage_en.html, HomePage_fr.html, etc.
Other web frameworks may have similar features. If you're using raw JSP/Servlets, I'm afraid you're pretty much on your own.
But it's totally possible to implement your own templating system. For example, you could use a set of Freemarker templates, and load the one that matches the desired locale.

How Can I rewrite SEO friendly URL on STRUTS?

We have a website which is coded Java with Struts Framework. The WebSite's Urls are not seo friendly. All of them are like below
../buyerApplication.do&companyId=2323
Now We want to make these URLs SEO friendly and I searched and found these solutions:
tuckey.org/urlrewrite : but i don't rely on this system.
adding
title end of link after '&' such as
"../newsId=33233&does-art-in-the-city-equal-art-for-the-city"
: In this solution I am not sure it
works well.
I am waiting your sugestions to solve this problem best.
I actually used URLRewriter (http://tuckey.org/urlrewrite/), which you referenced in your original question. It was very easy to set up and filled my needs perfectly.
To the point, you need a Filter for this.
If you want to keep your existing application's architecture, you'll need to define and create a set of rules to convert unfriendly urls to friendly urls and let the filter convert it and forward the request to the unfriendly url.
If there is no means of modifying an existing application but you want to create a new application based on this idea, you could consider to having a single page controller which translates the HttpServletRequest#getPathInfo()/getRequestURI() to execute the appropriate action class (command pattern) and finally forward the request to the appropriate JSP page. Not sure how that would fit into Struts as I haven't worked with Struts previously.
For what it's worth, you can also look at the REST plugin http://struts.apache.org/2.x/docs/rest-plugin.html, which amongst other things will make your URLs more friendly

Categories

Resources