Is it really a best practice to use jstl out tag?
I remember working on a project with a group of developers and they always wanted static
html text to be inside of an out tag (<c:out value="words" />
). I don't remember why
this was the case.
Is this really a best practice when building jsp pages? What are the advantages/disadvantages of such an approach?
Asked by: Stuart697 | Posted: 28-01-2022
Answer 1
It is a terrible idea for static text. You then have no barrier as to what is static and what is dynamically generated.
Besides which, on Servlet Spec 2.3+ you can have dynamic text mixed with static text as:
This is static, not ${dynamic}
text.
The only reasons to use c:out
tags, in my experience:
You're using an older servlet spec, and need them to output DYNAMIC text in some fashion
You want to escape
HTML
output to avoid using<>
, etc, replacing ampersands with their control codes, etc.
Otherwise, having them use static text confuses the programmer or maintainer...now where did I put that EL? It was in a c:out
tag...but so was fifty other lines of static text!
Answer 2
If you're just printing out plain text it's better to do it in HTML. The advantage of the c:out
tag is that you can evaluate expressions inside the tag.
<c:out value="Hello ${user.firstName} ${user.lastName}"/>
Answered by: Dainton107 | Posted: 01-03-2022
Similar questions
java - Using sqls in JSP - What is the best practice?
Say, You have an application which lists down users in your application. Ideally, if you were writing code to achieve this in Java, irrespective of what your UI layer is, I would think that you would write code which retrieves result set from the database and maps it to your application object. So, in this scenario, you are looking at your ORM / Data layer doing its thing and creating a list of "User" objects.
Let...
oop - Why is using a class as a struct bad practice in Java?
We recently had a code review . One of my classes was used so that I could return/pass more than one type of data from/to methods . The only methods that the class had were getters/setters . One of the team's members ( whose opinion I respect ) said that having a class like that is bad practice ( and not very OOP ) . Why is that ?
java - Is it good practice to replace Class with Class<? extends Object> to avoid warnings?
In a bunch o' places in my code, I have something like this:
public Class mySpecialMethod() {
return MySpecialClass.class;
}
which causes the warning
Class is a raw type. References to
generic type Class should be
parameterized.
But, if I replace
Class
with
Class<? extends Object>
java - Best Practice For Creating Stub Files With Ant
I'm trying to create myself a little build script to build a project for me - creating the desired directories, downloading any needed jars via Ivy/Maven and creating some stub files.
The stub files part is what I'm not too sure on what to do. To take an example, I would want the build script to create a web.xml in a WEB-INF folder. What would be the best way to do this though? The only way I know how is to have...
java - Best Practice Updating DB Records
Say you retrieve 100 records, and display them on a page. The user only updates 2 of the records on the page. Now you want to update only the two records, and not the other 98.
Is it best to have one submit on the page, then somehow know which 2 are updated, then send only those two to the db for an update?
What does the "somehow" look like?
Or, would you have an update-submit button for each row, a...
java - Best practice for ignoring year in date for date ranges
I need to model some information about seasons, and need to track them based on start / end date in a year-agnostic way. I.e. I need to allow users to define summer as being between, say, May 15th and September 10th, and do that for all years.
I'll need to do a lot of checking of the type isThisDateInSeason). All the date manipulation functions (i.e. Date, Calendar) seem to only work with valid dates, i.e. incl...
logic - A java practice problem
I came across this problem in javabat(http://www.javabat.com/prob/p183562):
We want to make a row of bricks that
is goal inches long. We have a number
of small bricks (1 inch each) and big
bricks (5 inches each). Return true if
it is possible to make the goal by
choosing from the given bricks. This
is a little harder than ...
java - Using sqls in JSP - What is the best practice?
Say, You have an application which lists down users in your application. Ideally, if you were writing code to achieve this in Java, irrespective of what your UI layer is, I would think that you would write code which retrieves result set from the database and maps it to your application object. So, in this scenario, you are looking at your ORM / Data layer doing its thing and creating a list of "User" objects.
Let...
java - Spring best practice for locking domain objects?
Using EJB entity beans you can configure the bean so that when a thread has access to an EJB entity bean, no other threads can access the EJB bean. The container will block other threads until the thread with the lock is finished with the bean. Is there a "Spring way" to do this? Or do you have to just use the standard Java concurrency synchronization approaches to handle this?
java - Spring Webflow Best Practice
I have a java web application which uses spring webflow as framework. I have a problem with processing data on a plain flow xml. When the processing gets more complicated I find it hard to implement using the flow xml of the web flow. I was considering of using controllers to perform these operations. How do I do this. Have no Idea in using controllers in web flow. And from controllers can I jump to the flow xml too to co...
Best practice to look up Java Enum
We have a REST API where clients can supply parameters representing values defined on the server in Java Enums.
So we can provide a descriptive error, we add this lookup method to each Enum. Seems like we're just copying code (bad). Is there a better practice?
public enum MyEnum {
A, B, C, D;
public static MyEnum lookup(String id) {
try {
return MyEnum.v...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)