-
Notifications
You must be signed in to change notification settings - Fork 0
For Developers
First of all, please note this library is not a bootstrap to deploy a website. This includes only the base class to allow the Model-View-Controller mechanism to work.
For better management of your website, you will probably need to build a bootstrap in top of the library. Here's a way to handle this. This is not the only way, only an exemple to help you.
Depending on how your website must work, you'll need a "BasePage". That means a page all your webpages will subclass, instead of subclassing directly Controller
.
This way, you'll be able to set some basic methods that will be available for all subclasses.
We also set the localization here. See Internationalization for more information.
public abstract class BasePage extends Controller {
public abstract String getTitle();
public ResourceBundle getResourceBundle()
{
ResourceBundle rb = ResourceBundle.getBundle(
"org.mdl.web.translate.Localization", getLocale());
return rb;
}
public Locale getLocale()
{
String lang = getRequest().getParameter("lang");
lang = (lang == null) ? lang = "en" : lang;
return new Locale(lang);
}
@Override
public void build(Template tpl) {
tpl.localize(getLocale(), getResourceBundle());
tpl.assign("title", getTitle());
}
}
Note that we don't define any CPath
or CError
annotations here, to force the system to ignore this one.
Now we make a base class, let's make the welcome page.
@CPath(path = "index.html")
public class WelcomePage extends BasePage
{
@Override
public void build(Template tpl)
{
super.build(tpl);
tpl.display("/WEB-INF/pagelets/index.jsp");
}
@Override
public String getTitle() {
return getResourceBundle().getString("Welcome");
}
}
This page extends BasePage
and implements getTitle()
. Because this is a subclass of BasePage
and we use super.build(tpl);
, the ${title}
will be set automatically.
When we set the JSP to display (by calling tpl.display(String)
, make sure to store your JSP within the /WEB-INF/
directory. This way, users will not be able to access directly to your pagelets from outside.
You can also set a new abstract subclass to be able to handle secure access to a webpage.
Here's an example that protects all controllers that subclass this class to be accessed from others users than "toto".
public abstract class SecurePage extends BasePage {
@Override
public void build(Template tpl) {
super.build(tpl);
String user = (String) getRequest().getSession().getAttribute("user");
if (!user.equals("toto"))
throw new UnauthorizedAccessException(
"Only toto can access this page!");
}
}