Show Struts2 error message on an HTML webpage

I have included error and message handling in my Struts2 action class using the addActionMessage and addActionError methods. I am now looking for code that will allow me to display these messages and errors on the HTML page. Is it feasible to do so?

Answer №1

To achieve a clean display of error and message responses in your JSP file, consider placing the <s:actionerrors /> and <s:actionmessages /> tags within specific div containers. For instance, you can use a red container for errors and a green one for messages. Remember to check if there are any errors or messages before displaying the div:

<s:if test="hasActionErrors()">
   <div class="feedError">
       <s:actionerrors />
   </div>
</s:if>

<s:if test="hasActionMessages()">
   <div class="feedOk">
       <s:actionmessages />
   </div>
</s:if>

For styling purposes, define the CSS properties for the error and message containers:

.feedError{
    width: 100%;
    border: 10px solid red;
}

.feedOk{
    width: 100%;
    border: 10px solid green;
}

If you are unable to access Action Errors and Messages directly from HTML, consider using an alternative view technology like FreeMarker Template or Velocity. Here's an example using FreeMarker Template (not tested):

<#if (actionErrors?size>0)>
    <div class="feedError">
        <@s.actionerror />
    </div>
</#if>

<#if (actionMessages?size>0)>
    <div class="feedOk">
        <@s.actionmessage />
    </div>
</#if>

For Velocity (again, not tested), the code could look like this:

#if( $actionErrors.size() > 0 )
   <div class="feedError">
   #foreach( $msg in $actionErrors )
       [$msg]<br />
   #end
   </div>
#end


#if( $actionMessages.size() > 0 )
   <div class="feedOk">
   #foreach( $msg in $actionMessages )
       [$msg]<br />
   #end
   </div>
#end

To restrict the use of scriptlets in JSP files, you can configure this behavior in the web.xml file by adding the following code:

<jsp-config>
        <jsp-property-group>
                <url-pattern>*.jsp</url-pattern>
                <scripting-invalid>true</scripting-invalid>
        </jsp-property-group>
</jsp-config> 

While using JSP with Struts2 is convenient, it's not the only option available. Explore other view technologies for a different approach to handling dynamic content in your web application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop cascading styles when using nested rules in LESS to prevent unintended style application

In the process of developing a sophisticated front-end system using plugins, we are exploring different methods for composing CSS rules. Currently, we are considering two main approaches: Including parents in the class name Nesting parents in the selecto ...

Increase the options available in the dropdown menu by adding more selected items, without removing any already selected

I came across this code on the internet http://jsfiddle.net/bvotcode/owhq5jat/ When I select a new item, the old item is replaced by the new item. How can I add more items without replacing them when I click "dropdown list"? Thank you <select id=&qu ...

The hideCol method does not work on jqGrid

Encountering a problem with the hidecol method in jqGrid. Despite calling the method, nothing seems to happen. Using version 4.5 of jqGrid. The table is created as follows: // Table creation using jqGrid $('#shipTable').jqGrid({ data: tabl ...

Update array when checkbox is selected/deselected

Using angular-google-maps, I am creating a map with various types of markers. Below the map, there are checkboxes in a simple form: <div class="mapOptions"> <form action="" class="form-inline" style="text-align:center"> <label for=""& ...

Show input field depending on chosen option

I'm looking to create a dynamic form where specific input fields are displayed based on the selection made in another field. For example, if "gender: male" is selected, the input field for "blue" should appear, and if "gender: female" is selected, the ...

Discover the method for measuring the size of a dynamically generated list

I'm currently working on a chat box that displays messages as a list. One issue I'm facing is that when I click on the start chat button, the chat box opens but the length of the list always remains zero. How can I resolve this problem? $(docu ...

Utilize NodeJS to redirect external asset requests to the local filesystem

Consider this scenario: We are tasked with fetching an image from an S3 bucket. <img src="https://s3-us-west-2.amazonaws.com/{BUCKET}/logo.png" /> Due to limited internet connectivity, I must find a way within my Express server to redirect requests ...

What is the best way to set a checkbox to null instead of false using Angular?

I am currently developing a filtering system that allows users to select different parameters to filter through a list of items. For instance, the item list could be a collection of dishes with filters represented as checkboxes labeled as vegan, vegetaria ...

A single pixel border surrounding an odd number of divs

I've been struggling with a persistent issue and I'm determined to find a solution. My goal is to achieve the following design using css: https://gyazo.com/c8ae39ebc4795027ba7c1067a08d3420 Currently, I have an uneven number of divs styled as fo ...

When arriving on a page via an HTML anchor tag, the CSS style does not appear. How can I "reinstate" it?

I have set up a website with an index.html page that links to a contact.html page using anchor tags. On the contact.html page, there are anchor tags that should navigate the user back to specific sections of the index.html page. The navigation works fine, ...

Refining Flask-Generated Table Content with jQuery Filters

I'm currently attempting to render a Jinja2 template that showcases an HTML table and enables dynamic filtering to search through the table content. Unfortunately, I'm facing issues with getting the search functionality to work properly. While th ...

What is the best way to align a div of variable size in a container of variable size using CSS?

My website features a 3-level structure that consists of a "workingArea" and an "imageContainer" within it, containing an image. <div class="workingArea"> <div class="imageContainer"> <img class="theImage" /> </div> </div> ...

Bootstrap 5's h-100 feature functions flawlessly in Chrome, however, it encounters issues when used with a group

Can you please refer to this question: Issue with Aspect Ratio of Images in Bootstrap 5 due to Padding After applying the h-100 solution, everything looks great on Chrome. Unfortunately, when I check it in Safari, the aspect ratio of the image gets distor ...

Width of the `div` element is expanding

I'm encountering an issue where a div container is automatically stretching to the full width of the page, instead of adjusting to fit the content within it. Below is the HTML code snippet: <!doctype html> <html> <!-- Head --> <h ...

Implementing a two-column infinite scrolling feature using ISCroll

I am looking to incorporate multi-column infinite scrolling using IScroll infinite scrolling. I want the content of my HTML to appear as follows: <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> ...

Adjust the div height to 100% in Bootstrap to center text, even with the presence of a navbar

I'm currently using Bootstrap 5 and facing an issue with getting text centered vertically on the page. I attempted to make the container of the div take up 100% of the page by using h-100, but it resulted in overflow due to the navbar occupying some s ...

The dimensions of the cards adjust automatically when the flex direction is set to row

My cards, created in CSS and React.js, have a set height and width. However, when I change the flex direction from column to row, the cards automatically shrink in size. Why is this happening? Card's CSS and JS code CSS .card{ height: 50%; w ...

Tips for transferring simple CSS formatting to an independent stylesheet

As part of a university project, I am working on a basic website. Initially, I used in-line styling for the website design. However, the subject coordinator has now changed the requirements and we are only allowed to use an external CSS stylesheet. Most of ...

Unable to get jQuery accordion to function as expected when using content retrieved through AJAX requests

Seeking assistance with a malfunctioning accordion that is not working after receiving content from another page through AJAX. The problematic page in question can be found at: Upon loading the page, it sends a default city name to processtempo.php which ...

Having trouble aligning two divs on the same line and adjusting the controls to expand their width when there is extra space

I am currently creating an application using React and Material UI, incorporating the autocomplete control feature. Take a look at the code sandbox I have set up. Here are some concerns: I am aiming to have two autocomplete controls displayed on the same ...