I'm working with Redmine for the first time and was able to successfully install it locally. I haven't used Ruby before and I come from a Java background.
I am able to run the application by going to -
http://localhost:3000/projects
The question is, where can I find the html file (if it exists) that corresponds to http://localhost:3000/projects. In Java, we can do this by looking at web.xml or the relevant Spring configuration file and see how the URL is mapped to a servlet or controller. How to do this in Ruby?
The counterpart of web.xml is the routes.rb and the config.rb files in ruby. You'll find them in the config directory. The routes.rb defines which controller and action (much like servlets) will handle a certain request (URL). And since Rails has predefined conventions, all the html files go in the folders named after the controllers in the views directory and by convention the html file with the same name as that of the controller's action that has been invoked will be rendered as the response.
But all of this can be overridden if desired.
This is a nice place to start understanding Rails: http://guides.rubyonrails.org/
Note that Ruby is not the same as Rails.
Ruby, like Java, is a programming language. It appeared in 1995. For example, the following is a script/program you can execute from the command line.
#!/usr/bin/env ruby
puts "Hello World"
Rack is a web server interface for Ruby. It handles the HTTP protocol, and allows one to write web applications in Ruby by making it easy to parse HTTP requests and send HTTP responses.
Rails is a web framework with powerful conventions, patterns, and tools for developing web applications in Ruby. Some part of it uses Rack. It appeared in 2004. Sinatra is an example of another web framework that uses Rack.
What's the equivalent of web.xml in Ruby?
It does not exist.
What's the equivalent of web.xml in Rack?
Probably config.ru.
What's the equivalent of web.xml in Rails?
config/routes.rb and config/application.rb. Please refer to Configuring Rails Applications.
Routes
To figure out which html file corresponds to http://localhost:3000/projects, look in config/routes.rb. If you see
resources :projects
then it is handled by the index action in ProjectsController with the view at app/views/projects/index.*.
Rails follows convention over configuration principle so all views can always be found in
app/views/
and the one you are looking for should be (depending on Redmine template processor)
app/views/projects/index.html.erb
Also a convention is that view files are named like
path/to/view/_action_name_._content_type_._processor_
In Rails you can find all the mappings to the routes of your web app looking at the routes.rb file in the config folder inside your project folder.
For example, if you want to configure the index page in your project, remove the index.html.erb in the public folder and do like:
root :to => "yourController#someAction"
Understanding routes is not an easy task for someone coming from a Java background. But this should help.
Related
If I wanted to read a jsp like:
File f = new File("my.jsp");
and then I want it to produce the expected HTML as say a String (byte[], OutputStream, whatever), How would I do that?
I assume there is some transformer or something. Can anyone point me in the right direction?
A JSP File is translated and compiled as a Servlet class, so it not directly generate a html File, the reason is a JSP as a Servlet could generate dynamic html depending of the parameters in the request, in the tomcat server it use the Jasper 2 JSP Engine to implement the JavaServer Pages 2.1 specification that process the JSP, so you can check it but I don't recommend you do that.
Maybe what your really want is a template mechanism, for example you have a text file similar to the jsp with similar sintax to the EL that can access some objects, so you can use the template engine and bound your object with the text and produce your html. If it works for you, you can use Apache Velocity - HTML example or FreeMaker - HTML example.
Jasper is the JSP compiler used by Tomcat, it takes the JSP pages themselves, parses them, and generates Java code (in the form of a servlet, or at least it used to be) which writes the JSP's output to the servlet response. That Java code would then be compiled and executed against the containers implementation of the Servlet API.
Jasper is a standalone library, so in theory you could instantiate its classes inside your own harness, point it at a JSP, generate the servlet code, then compile and execute it. However you'd need to pass in your own mock implementations of the Servlet API interfaces, as it's these that will be called to determine request parameters (HttpServletRequest), and to write the response (HttpServletResponse). You'd need to provide a version of HttpServletResponse that will buffer up the output for you to grab later.
From the Tomcat 6 API, this bits your want are in the org.apache.jasper package.
Taking a look at at it you'd probably want to create a JspCompilationContext which points to your JSP, call createCompiler(), and take it from there.
As far as mocking out the Servlet API goes, you might get what you need from the stub implemetations provided by the Spring Framework for testing purposes
Velocity and FreeMarker are both excellent for this, and much easier for basic templating.
However, it can be done.
I have an example explained here: Tomcat JSP/JSTL without HTTP
It's obviously easier from within a web app (as all of the jars will be there), but it can be done outside, you'll just need to identify and the appropriate jars to your program.
I want to build a java web application and I don't have any background how to do that.
Can you plz tell me what is the starting point to do that and where can I found useful open source codes that I can use them to design my web application.
There are many different frameworks and without more information it's difficult to know what would suit you.
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#Java is a good starting point.
You have to know concepts such as Servlet, Servlet Container, Application Server(such as Apache tomcat) and little information about Html.
Exist several book for this goal, my opinion is : you start by a book related to Jsp/Servlet concept, these books good explained.
Here you can learn how java web applications work and here is a very basic java web application example to get you started. I hope this helps :)
You should follow the Java EE tutorial, its Web Tier part. I think it's the fastest way to get knowledge that would allow you to understand the base concepts...
The minimal structure of a web application is the following:
/WEB-INF
/classes - stores the compiled Java classes your webapp uses
/lib - contains the additional libraries your webapp may need to run
web.xml - key file in every webapp; explained below
web files and folders (HTML/JSP/CSS/Javascript)
You may want to start out with Eclipse for Java-EE, since it automatically creates the webapp structure for you, so it's the perfect place to start learning, in my opinion; you can find it here.
After you install, the basic steps to create your web application are:
Create your project by accessing File > New > Dynamic Web Project.
Name your project, click Next, Next and check the Generate deployment descriptor checkbox. Now hit Finish.
Now that the structure is created, your main points of interest will be:
Deployment Descriptor - Is an overview of your web.xml file. Here you can declare all your servlets and their URL paths, you can point to specific error pages triggered by specific codes (e.g 404, 500) or exceptions that occur in your Java/JSP code (e.g NullPointerException, FileNotFoundException), plus do many other things to enhance your webapp. You can trigger between text and graphical XML editing in the bottom-left of the code window.
Java Resources - Here you define your Java classes and servlets. The main role of a Java class in a webapp will be to collect and process data. For example you can define your own math class that exposes methods which do basic calculations. A servlet will usually call one of these classes and output the result to the response output stream. Be sure to provide a solid project structure with the help of packages.
WebContent - this will contain all the web pages your webapp will show, including scripts, images and stylesheets. You are free to create your own folder structure in this section.
Some useful tutorials to get you started:
HTML
JSP
Servlets, Server setup
CSS
Once you're done with your webapp, you can either Run it on a server directly from Eclipse, or you can export it as a WAR file and deploy it on the server of choice, which is usually done by copying the WAR file in the webapps folder.
Finally, try to experiment with all the webapp features Eclipse exposes to you. Good luck!
I have to call a servlet written in Java from Clojure web application, and I don't understand how to do that.
Developing a webapp in Java, I had to describe all mappings in web.xml. In Compojure, I see, I must describe routes. So, can I bind the Java servlet to one such route?
Sorry if my question is stupid, but I've searched a lot and didn't find an answer; I'm just starting to develop for the web.
Two helpful pointers:
There's an example on how to generate the Vaadin servlet completely from Clojure on github
And here's a SO question on how to map a java filter to routes
I suggest, there is only one way to cope with my tasks. I have to manually instantiate Java servlets in Clojure web app and form their request and response parameters. Test package for ring.util.servlet library describes this approach in details, but in case of turning Ring handler into a Java servlet.
In case of deploying Clojure app and Java servlets to servlet container separately there's no need to define additional Compojure routes or Ring handlers to paths mapped by container.
Please tell me whether my suggestions are incorrect.
I have a Ruby on Rails application with much business logic contained in the models. I also have a backend process in Java that needs to use the same business logic. How can I package the Rails app into a jar that I can call from Java (using JRuby)?
I need to access the code directly in Java for performance reasons. Performing an HTTP request has too much overhead. Using a message queue won't work as the access needs to be synchronous.
Have you looked at Warbler?
https://github.com/nicksieger/warbler
This will help you package your rails app into a war. Would that help you?
Alternatively, you could just look at the files warbler generates: it creates a .class file for each .rb file, and then generates a file that includes the .class file. Perhaps you could take them and package them into a jar.
Also, for some info on how to access activerecord from jruby outside the rails app, look at section 2.14 from the book 'JRuby Cookbook'. Basically, it involves reading the config from database.yml, and opening up a DB connection using ActiveRecord.establish_connection, then you are ready to access your models by just requiring them.
I have a multi WAR web application that was designed badly. There is a single WAR that is responsible for handling some authorization against a database and defines a standard web page using a jsp taglib. The main WAR basically checks the privileges of the user and than based on that, displays links to the context path of the other deployed WARS. Each of the other deployed WARs includes this custom tag lib.
I am working on redesigning this application, and one of the nice things that I want to retain is that we have other project teams that have developed these WAR modules that "plug into" our current system to take advantage of other things we have to offer.
I am not entirely sure how to handle the page templates though. I need a templating system that would be easy enough to use across multiple wars (I was thinking of jsp fragments??). I really only need to define a consistent header and main navigation section. Whatever else is displayed on the page is up to the individual web project.
Any suggestions?
I hope that this is clear, if not I can elaborate more.
Have done something similar in the past using Sitemesh
we defined a new web app called skins-app which only has the common header, footer, navbar which all other need.
Sitemesh is configured via a file named WEB-INF/decorators.xml in the skins-app
then in any consuming webapp, you add a WEB-INF/decorators.xml as well.
And point your pages to be 'decorated' by the skins from another app
<decorator name="main" page="/decorators/layout.jsp" webapp="skins-app">
<pattern>/*</pattern>
</decorator>
You can have detailed include/exclude as well in your consuming webapp, if any pages needed to be excluded from the 'decoration'. Take a look at the Visual Example on the Sitemesh link page.