2

I am going to pass error message from inside the Java class to JSP and this error message is written in .properties file.

I am using action class method addActionError(result) to display that error message but it is displaying the message as error.register.bademail in JSP. This is not my written message.

Java class:

package com.uttarainfo.s2;
public class Model {

    public List<String> register(RegBean bean) {
        if(bean.getEmail().equals("bond@gmail.com"))
            return "error.register.bademail"; i want to return this key 
        else
            return "success";
    }

}

Action class:

if(result.equals(SUCCESS))
    return SUCCESS;
else
{
    addActionError(result);
    return "failure";
}

This is JSP code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Register</h1>
    <s:form action="register"  method="post" enctype="multipart/form-data">
        <s:textfield key="bean.uname"/>
        <s:textfield key="bean.email"/>
        <s:password key="bean.pwd"/>
        <s:password key="bean.rpwd"/>
        <s:file key="bean.pic"/>
        <s:submit/>
        <s:actionerror/>
    </s:form>
</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
user2767354
  • 109
  • 1
  • 1
  • 9

2 Answers2

0

Try

addActionError(getText(result));

getText() is used to retrieve localized messages form resources. All you need to supply the key.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • You return result as a key, so the key is displayed. Translate the key via `getText()` to get a value that corresponds to the key which you define in the resource bundle. – Roman C Sep 28 '13 at 07:07
  • 1
    thank u that working but can u plz elaborate me if i use my return data type as arraylist how to return my key from the method – user2767354 Sep 28 '13 at 07:28
  • if you want to return a key like an arraylist, or other property which you should create in the action class and provide getter, in the jsp use OGNL to get the value or everything with the `s:text` tag. – Roman C Sep 28 '13 at 07:54
  • could u plz help me for the above updated code i have replaced my return type as list instead of string for this case how to return my key to the action plz explain me thnk u – user2767354 Sep 28 '13 at 08:22
  • but you didn't write the code for the bean, nor for the action, you don't have a configuration, I don't know how could I help? – Roman C Sep 28 '13 at 08:29
  • hi can u see my edit now i didn't get plz ask me what u want i will explain once again. – user2767354 Sep 28 '13 at 09:01
  • i know how to return value from action support but i am not returning form action support i want to return exception or some error messages from my javaclass when using my return type as List could u plz assist me i am sorry i can't able to understand u just ask me i will post my code thnk u for ur continues support – user2767354 Sep 29 '13 at 07:52
  • I don't know why did you change the return type, what do you talking to assist you create a List and put a error code to it? – Roman C Sep 29 '13 at 08:03
  • not like that when am retrieving the database value from the database table i will retrieve all the elements form database and will put inside list and this list i will forward to the jsp for this case while retrieving values from database table if any exception occurs or if the connection not established i want show that message to the user but i could not be able to return that message from javaclass because my return type is list – user2767354 Sep 29 '13 at 08:12
  • You should create an exception handler and result that returns the message you want you should keep the messages not in database but in the global resources, and this is not true that you can't return a message after the message is translated. – Roman C Sep 29 '13 at 08:19
  • you are telling that i have to create that in struts.xml and if any exception occurs i have to forward to that – user2767354 Sep 29 '13 at 08:27
  • why not? do you have a better solution? – Roman C Sep 29 '13 at 08:33
0

I want to see struts.xml as well as the properties file. please put the code.

EDIT: Ok, so what i understood from your edit is that you have to return a list (as you do in java) instead of string from the action class. If yes you can not do so. However,use struts-2-json plugin to return json objects to the view.

http://www.mkyong.com/struts2/struts-2-and-json-example/

Also , it does not matter what type of data is returned by your action method as you can only return string but what attributes you set in your action class. So, the attributes set in action class will be accessible in your view & for List(set in action class) you will have to use iterator.

Moreover if you have just stated with struts2 I will recommend you understand the MVC & role of getters & setters in it.

Can I use mvc without getters and setters?

Thanks for the input, if you get error then also you should return a string. with that string the mapped error page in struts.xml will be shown. For eg. while processing data you found it inconsistent then you can return a string "error" and in struts it appears as

<action name="yourAction" class="action.Action">
<result name="error">/Error.jsp</result>
</action>

And for exception handling which is what you should be looking for or will soon you can go to the link below http://struts.apache.org/release/2.3.x/docs/exception-handling.html

Please tell me if I answered your question.

Community
  • 1
  • 1
Amit Kumar
  • 2,685
  • 2
  • 37
  • 72
  • now that is working fine my question is if i use arraylist as return type how i will get the key from the method plz explain me thnk u – user2767354 Sep 28 '13 at 07:37
  • I don't get your question please be more descriptive. where do you want to return arraylist? – Amit Kumar Sep 28 '13 at 08:38
  • ok so what i understood from your edit is that you have to return a list (as you do in java) instead of string from the action class. If yes you can not do so. However,use struts-2-json plugin to return json objects to the view. Also , it does not matter what type of data is returned by your action method but what attributes you set in your action class. So, the attributes set in action class will be accessible in your view & for List you will have to use iterator. – Amit Kumar Sep 28 '13 at 10:47
  • hi i am very very sorry , here am returning error messages from the java class not from action class my question is to when am retrieving the elements from the database i will be using array list to put all the elements inside the list and i will return that list, return type as list in the java method at that time if there any error occurs how to return the key as above code,if u not get my question u can ask me once again thnk u for replying me .. – user2767354 Sep 28 '13 at 11:05
  • hi thnks for replying but my question is public List register(RegBean bean) { if(bean.getEmail().equals("bond@gmail.com")) return "error.register.bademail"; i want to return this key to the action page will it posible? bcz i don't know here am using list return type – user2767354 Sep 28 '13 at 17:47
  • As far as I know you can return a constant from ActionSupport class or a string not a key.However if you are still not getting it post the code in your files. – Amit Kumar Sep 29 '13 at 06:20
  • i know how to return value from action support but i am not returning form action support i want to return exception or some error messages from my javaclass when using my return type as List could u plz assist me i am sorry i can't able to understand u just ask me i will post my code thnk u for ur continues support – user2767354 Sep 29 '13 at 08:13
  • not like that when am retrieving the database value from the database table i will retrieve all the elements form database and will put inside list and this list i will forward to the jsp for this case while retrieving values from database table if any exception occurs or if the connection not established i want show that message to the user but i could not be able to return that message from javaclass because my return type is list – user2767354 Sep 29 '13 at 08:14
  • For exception occurring at runtime you can use Exception handling of struts2. I already gave you the link. Here it is http://struts.apache.org/release/2.3.x/docs/exception-handling.html. http://www.javatpoint.com/struts-2-exception-handling-exception-interceptor Tell me if you are not able to do what you want. – Amit Kumar Sep 29 '13 at 08:33