I have my Wicket 1.4 code to have a link to download a file which is generated programatically:
protected class MyWebResource extends WebResource {
public IResourceStream getResourceStream() {
.....
return new StringResourceStream(myString, "text/plain");
}
}
ResourceLink<?> downloadLink =
new ResourceLink<Object>("downloadLink", new MyWebResource());
Everything was good. Now I've upgrade to Wicket 1.5. Now WebResource doesn't exist any more.
I've searched the web for ages, surely this must be a simple problem which has a simple solution? Alas I can't find it.
The replacement is AbstractResource. Basically you should create ResourceResponse and do what you did in its WriteCallback.
See the specializations of AbstractResource in Wicket's code for examples.
See http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/ as well.
Related
Following the documentation here, I am trying to get i18n working on my AEM 6.2 instance:
Locale pageLang = currentPage.getLanguage(false);
ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLang);
I18n i18n = new I18n(resourceBundle);
The problem is, I always get a org.apache.sling.engine.impl.helper.NullResourceBundle which seems to be just an empty fallback bundle if nothing was found. I tried to preload the bundles in the OSGi configuration, but to no avail.
Even if my own translations wouldn't work, at least it should contain the predefined translations provided with the product. Though my own show up in the tool:
http://localhost:4502/libs/cq/i18n/translator.html
Any ideas why I don't get a valid resource bundle?
EDIT: I did a simple JSP test and there it works, so it is related to Sling Models where the code resides within a #PostConstruct method where the model was adapted from the SlingHttpServletRequest
With the help of the wcm.io mailing list and a snippet from it's code I was able to get this working:
Locale pageLang = currentPage.getLanguage(false);
SlingHttpServletRequest request = slingRequest;
SlingBindings bindings = (SlingBindings)slingRequest.getAttribute(SlingBindings.class.getName());
if (bindings != null) {
request = bindings.getRequest();
}
ResourceBundle resourceBundle = request.getResourceBundle(pageLang );
It seems that the injected request in SlingModels isn't the real request, but just a wrapper that doesn't contain the ResourceBundle. I then tried the #AemObject from wcm.io which also didn't work for me and then I asked in the mailing list what could be the problem.
I'm new to Spring MVC and Hibernate. I'm trying to start a project by following tutorials but I have been running into problems as my project structure is not consistent with the tutorials I am reading.
I have downloaded the latest STS and I do see the option of creating an Spring MVC project. However it is based on Spring 3 and still uses XML files. From what I have read it looks like there is a way to do it without XML files since Spring 3. I prefer annotations over XML files greatly.
How can I create a Spring MVC 4 application that is based on annotations and relies on xml files minimally?
EDIT:
I want to create a web project
Here is a squeletal example of full java configuration. You will need :
a class extending AbstractAnnotationConfigDispatcherServletInitializer to replace the old web.xml file
one or more #Configuration annotaded class(es) to initialize the root context (replaces the old applicationContext.xml)
one or more #Configuration annotaded class(es) to initialize the DispatcherServlet context (replaces the old dispatcher-servlet.xml)
This is the web.xml :
public class WebAppConf extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
// declare root context configuration classes
return new Class<?>[]{ RootConf.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
// declare servlet context configuration classes
return new Class<?>[]{ ServletConf.class };
}
#Override
protected String[] getServletMappings() {
// mapping of DispatcherServlet
return new String[]{"/"};
}
#Override
protected void customizeRegistration(Dynamic registration) {
// additional configuration, here for MultipartConfig
super.customizeRegistration(registration);
MultipartConfigElement multipartConf = new MultipartConfigElement("", 200000L, -1L, 0);
registration.setMultipartConfig(multipartConf);
}
}
RootConf will declare business model, service and dao beans and is not shown here.
ServletConf declares the controllers and servlet configuration :
#Configuration
#EnableWebMvc
// declare where to find annotated controllers
#ComponentScan({"org.example.web"})
public class ServletConf extends WebMvcConfigurerAdapter {
#Bean
MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
#Bean
ViewResolver internalViewResolver() {
// the view resolver bean ...
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
As said above, it is squeletal, but it comes from a working minimal example so you should be able to start with that and extend it at will. In my example, the above three classes live in a org.example.config package that will never be scanned for autodetecting other configuration classes or annotated beans.
Hope it helps ...
I know this doesn't answer your question fully, but hopefully the links will be useful.
WebApplicationInitializer - A 100% code based approach to configuration
as well as AnnotationConfigWebApplicationContext
Also, if you have the time, reading the relevant sections of Spring's MVC chapter of their documentation is helpful.
I Wish that this link will be helpful for you Spring security with annotation Mkyong
The latest versions of STS integrate the Spring guides from https://spring.io/guides directly, try the "Import Spring Getting Started Content" wizard. There are good guides included for creating a Spring Boot based web service, for example, among many others.
I have an interface that extends the com.google.gwt.i18n.client.Messages class, which I use for retrieving i18n messages in my GWT application. It looks like this:
public interface MyMessages extends com.google.gwt.i18n.client.Messages {
#DefaultMessage("Hello world")
#Key("message1")
String message1();
#DefaultMessage("Hello again")
#Key("message2")
String message2();
//...
}
Normally, I create an instance of it using GWT.create() like so:
private MyMessages messages = GWT.create(MyMessages.class);
However, this does not work with server-side code, only client-side code (it throws an error saying that GWT.create() is only usable in client-side code).
The answer to a similar question points to a separate library that you can download which will let you access the i18n messages on the server, but I don't want to download any extra libraries (this seems like a simple problem, there must be a simple solution).
In summary: How can I access my i18n messages in server-side code? Thanks.
On the server side you can use the standard Java localization tools like ResourceBundle.
Look here for a tutorial how to use it.
// Create a ResourceBundle out of your property files
ResourceBundle labels =
ResourceBundle.getBundle("LabelsBundle", currentLocale);
// Get localized value
String value = labels.getString(key);
The GWT specific way of creating an interface out of your property files and providing implementations via deferred binding can not be used on sever side Java.
If you are fearless and willing to spend the time, you can implement a code generation step to read your property files and generate implementation classes for your message interface. That's exactly what the Google GWT compiler does behind the scene.
I agree with Michael.. I was having this problem of trying to "localize" messages generated on the server.... but I decided to instead just throw an Exception on the server (because it is an error message which should only happen exceptionally) which contains the message code, which the client code can then look up and show the correct localized message to the user.
There's a great library for GWT internationalization gwt-dmesg. It allows you to 'share' .properties files between clent and server. However, project looks to be abandoned by author and you must recompile it manually for use with GWT versio >= 2.1.0.
GWT.create() can only be used in client-side code.
The good thing to do is that you provide your own I18NProvider class/interface, from which then you can extend to server side I18N factory and client side I18N factory read the same resource bundle.
After that you can simply use it all over your system, unify your code.
Hope that helps.
Following vanje's answer, and considering the encoding used for the properties files (which can be troublesome as ResourceBundle uses by default "ISO-8859-1", here is the solution I came up with:
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyResourceBundle {
// feature variables
private ResourceBundle bundle;
private String fileEncoding;
public MyResourceBundle(Locale locale, String fileEncoding){
this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
this.fileEncoding = fileEncoding;
}
public MyResourceBundle(Locale locale){
this(locale, "UTF-8");
}
public String getString(String key){
String value = bundle.getString(key);
try {
return new String(value.getBytes("ISO-8859-1"), fileEncoding);
} catch (UnsupportedEncodingException e) {
return value;
}
}
}
The way to use this would be very similar than the regular ResourceBundle usage:
private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)
Or you can use the alternate constructor which uses UTF-8 by default:
private MyResourceBundle labels = new MyResourceBundle("es");
Hi I am new to Spring MVC i am following Spring reference documentaion I have doubt in view resolver. Here is my sample code.
#Controller
#RequestMapping("/form")
public class MyController {
#RequestMapping(method = RequestMethod.GET)
public String setupForm() {
// do my stuff
return "myform";
}
#RequestMapping(method = RequestMethod.POST)
public String processForm(ModelMap model) {
// process form data
model.addAttribute("notification", "Successfully did it!");
return "redirect:/form";
}
}
here i am using two controllers, the first one returns "myform" and second one returns "redirect:/form". My question is, what is the difference between these two and how it works?
If you are familiar with jsp servlet, I think you can know the difference between redirect and forward, or you can get tons of answers from google. Then I want to explain a bit about how Spring does this. in setupForm method, it returns myform, then according to your view resovler configuration, it will try to find a myform.jsp or another likes this, and if your view resovler is internalresourceviewresovler, Spring will do a forward automatically and try to find this jsp in web-inf directory, if not, you have to specify a forward prefix. and for processForm method, that after return redirect:/form, it will force browser to send a new request /form to server which can be got by spring mvc and it will handle it with the related method.
Hi Can anyone let me know where I can find sample RESTFul APIs so that I can have hands on experience working on them using GET/POST/DELETE methods.
Thanks.
If you have an idea about Java, Servlet, Wildfly / Tomcat server, Mysql then you can create a basic program for Get / Post Method and deploy on the local server.
You will learn to create basic API also. The best way you install eclipse and integrate server. With Eclipse, you can easily create Servlet as it provides template and web.xml generate automatically.
Create a basic object Class. Call it in servelet fill some data.
Convert it to JSON by using GSON
#WebServlet(asyncSupported = true, urlPatterns = { "/School_getEmployee" })
public class School_Employee_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Gson gson = new GsonBuilder().serializeNulls().create();
String json = gson.toJson(Object);
PrintWriter out = response.getWriter();
out.println(json);
}
Now you can use this servlet as API for testing.
You can use JSON-SERVER, I'm using these to make GET/PUT/POST/DELETE