Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post error messages to the HTML from Struts2 outside of Action class
    primarykey
    data
    text
    <p>I have a register program. When I insert a record on the database, I'll instantiate a class and invoke the method <code>insert()</code>. When I insert a the same record, of course there is a duplicate data error and tons of error messages. I want to capture it with <code>try</code> and <code>catch</code>. I can do that. However, I have no idea how to display the message to the JSP. </p> <p>What I have understand, in an action class, the <code>validate()</code> method and the <code>validation.xml</code> is run first. The insert duplicate error happened after those methods are invoked. </p> <pre><code>import com.opensymphony.xwork2.ActionSupport; import lotmovement.business.crud.InsertUserProfile; import lotmovement.business.entity.UserProfile; import org.apache.commons.lang3.StringUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class RegisterAction extends ActionSupport { private static String userId; private static String password; private static String firstName; private static String lastName; private static int securityLevel; @Override public String execute() { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); InsertUserProfile iup = (InsertUserProfile)context.getBean("insertuserprofile"); iup.Insert(); return SUCCESS; } </code></pre> <p><strong>Here is my insert user profile method of</strong> </p> <pre><code> public void Insert() { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); UserProfile up = (UserProfile)context.getBean("userprofile"); RegisterAction ra = (RegisterAction)context.getBean("registeraction"); EntityStart es = (EntityStart)context.getBean("entitystart"); es.StartDbaseConnection(); up.setUserId(ra.getUserId()); up.setFirstName(ra.getFirstName()); up.setLastName(ra.getLastName()); up.setPassword(ra.getPassword()); up.setSecurityLevel(ra.getSecurityLevel()); es.StartPopulateTransaction(up); es.CloseDbaseConnection(); } </code></pre> <p><strong>This is my JSP:</strong></p> <pre><code>&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;link rel="stylesheet" type="text/css" href="CSS/register.css"&gt; &lt;title&gt;Register&lt;/title&gt; &lt;s:head /&gt; &lt;/head&gt; &lt;body&gt; &lt;s:form method="POST" action="register" &gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;s:actionerror/&gt; &lt;s:fielderror/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;s:textfield label="UserID" key="userId" maxLength="20"/&gt; &lt;s:password label="Password" key="password" maxLength="20"/&gt; &lt;s:password label="retype-Password" key="retypepassword" maxLength="20"/&gt; &lt;s:textfield label="Firstname" key="firstName" maxLength="20"/&gt; &lt;s:textfield label="Lastname" key="lastName" maxLength="20"/&gt; &lt;s:textfield label="SecurityLevel" key="securityLevel" maxLength="20"/&gt; &lt;s:submit value="Register"/&gt; &lt;/s:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload