0

I couldn't able to load my action class "search" method. I get the below error while typing inside my textbox with id paliName.

Error - GET http://localhost:8080/Newgen/searchName.html?term=a 404

struts.xml

<struts>
    <constant name="struts.action.extention" value="html" />
    
    <package name="json" namespace="/" extends="json-default">
        <action name="searchName" class="com.nexgen.web.PaliAction" method="search">
            <result type="json">
                <param name="root">jsonData</param>
            </result>
        </action>
    </package>
</struts>

register.jsp

<s:textfield id="paliName" name="paliName" label="Palindrome" maxlength="100"/>

// A $( document ).ready() block.
    $( document ).ready(function() {
        $("#paliName").autocomplete({
            source: function (request, response){
                $.getJSON('http://localhost:8080/Newgen/searchName.html?term=' + request.term, function(
                        data){
                            response($.map(data.names, function(value,key){
                                return{
                                    label: value,
                                    value: key
                                    };
                                }));
                    });
                },
                minLenght:1,
                delay: 100
            });
        
    });

PaliAction.java

public class PaliAction extends ActionSupport implements ModelDriven<Palindrome> {

    private static final long serialVersionUID = -6659925652584240539L;
    private Map<String, Object> jsonData = new HashMap<String, Object>();
    private Palindrome user = new Palindrome();
    private String term = "";
    
    public Palindrome getModel() {
        return user;
    }
    
    /**
     * To search for autocomplete.
     * @return String
     */
    public String search()
    {   
        jsonData.put("names", userDAO.searchName(term));
        return SUCCESS;
    }

    public Map<String, Object> getJsonData() {
        return jsonData;
    }

    public void setJsonData(Map<String, Object> jsonData) {
        this.jsonData = jsonData;
    }

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }
    
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Manoj
  • 41
  • 5
  • Can you explain the error? – Roman C Nov 10 '20 at 07:13
  • I was trying to make an autocomplete textbox which loads the available options from the database. With the above jquery function I was trying to hit an action class in struts.xml. "searchName"is the action name in struts.xml.But it couldn't get the class. 404 error. – Manoj Nov 10 '20 at 17:52
  • I have made autocompleter with jQuery couple of years before. See [this](https://stackoverflow.com/a/22202105/573032) answer. – Roman C Nov 10 '20 at 21:28
  • @RomanC can you tell how your action class got configured in struts.xml. I have configured in above mentioned way. If i give other action name in getJSON function it goes to that action but if i give the action name that is present inside "" it gives 404 error. – Manoj Nov 12 '20 at 05:50
  • It doesn't matter how you configured your action. If you configured it wrong then it doesn't work of course. Because you didn't post the error stacktrace it's impossible to say what's the issue. You can see [this](https://stackoverflow.com/a/27147358/573032) answer for example to configure struts action. – Roman C Nov 12 '20 at 19:51
  • It worked after adding a jar "struts2-json-plugin-2.5.25". – Manoj Nov 12 '20 at 19:59

0 Answers0