Grigory
Grigory

Reputation: 1024

A method from struts2 action class is not executing

I'm porting a webapp from struts 2.0 to 2.3. I cannot make DynamicMethodInvocation work. I have an action class with the add() method. Submitting a form on register!add.shtml does not execute this method. I cannot find any reason for this.

Here are the jars I use:

struts2-core-2.3.16.jar
xwork-core-2.3.16.jar
struts2-spring-plugin-2.3.16.jar

Here's a part from struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.objectFactory" value="spring"/>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.url.includeParams" value="none" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <constant name="struts.action.extension" value="shtml"/>

    <package name="item" extends="struts-default" strict-method-invocation="true">
        <action name="register" class="profileAction">
            <!--<result>/WEB-INF/jsp/register.jsp</result>-->
            <result name="success">/WEB-INF/jsp/register_success.jsp</result>
            <result name="input">/WEB-INF/jsp/register.jsp</result>
            <result name="error">/WEB-INF/jsp/register.jsp</result>
            <allowed-methods>execute,add,save</allowed-methods>
        </action>

There is also a simple struts-beans.xml. There are no other files like .properties.

Anyone?

PS Here's the action code. But I don't think it matters. I tried put a breakpoint on the first line. But the debugger just doesn't get there.

public String add() {
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("<person>\n")
                .append("<fullname>").append(getLastName()).append(" ").append(getFirstName()).append("</fullname>\n")
                .append("<login>").append(getLogin()).append("</login>\n")
                .append("<email>").append(getLogin()).append("</email>\n")
                .append("<phone>").append(getPhone()).append("</phone>\n")
                .append("</person>\n");
        URL url = new URL(getCreatePersonURL());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/xml; charset=utf8");
        OutputStreamWriter wr= new OutputStreamWriter(connection.getOutputStream());
        wr.write(sb.toString());
        InputStream content = connection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(content));

        String line;
        if ((line = in.readLine()) != null)
            if (!line.equalsIgnoreCase("<status>OK</status>")) {
                addActionError("Регистрация в данный момент не возможна"); // TODO обработка ошибок от zayavkaapi

                return Action.ERROR;
            }
    } catch (IOException e) {
        addActionError("Регистрация в данный момент не возможна"); // TODO обработка ошибок от zayavkaapi

        return Action.ERROR;
    }

    Customer c = new Customer();
    c.setKeyword(getKeyphrase());
    c.setPassword(getPassword());
    c.setLogin(getLogin());
    c.setEmail(getEmail());
    ContactPerson cp = new ContactPerson();
    cp.setFirstName(getFirstName());
    cp.setLastName(getLastName());
    cp.setMiddleName(getMiddleName());

    ArrayList<ContactInfo> cil = new ArrayList<ContactInfo>();
    ContactInfo ci = new ContactInfo();
    ci.setContactInfoTypeId("ADDRESS");// Адрес
    ci.setCountryId(getCountry());
    ci.setField2(getCity());
    cil.add(ci);

    ci = new ContactInfo();
    ci.setContactInfoTypeId("PHONE");// Телефон
    //String[] strings = getPhone().split(" ");
    ci.setField1(getCountryCode());
    ci.setField2(getCityCode());
    ci.setField3(getPhone());
    ci.setField4(getOfficePhone());
    //if (strings.length > 3)
    cil.add(ci);

    ci = new ContactInfo();
    ci.setContactInfoTypeId("EMAIL");// e-mail
    ci.setField1(getEmail());
    cil.add(ci);

    cp.setContactInfoList(cil);
    c.setMainContactPerson(cp);
    ArrayList<ContactPerson> cpl = new ArrayList<ContactPerson>();
    cpl.add(cp);
    c.setContactPersons(cpl);
    Long customerId = customerManager.createCustomer(c);
    customer = customerManager.getCustomerById(customerId);

    User user = new User(getLogin(), Md5Calc.MD5(getPassword()) , true, true, true, true, new GrantedAuthority[]{new GrantedAuthorityImpl("ROLE_CUSTOMER")});
    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(user, getLogin(), user.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(result);

    return Action.SUCCESS;
}

Upvotes: 0

Views: 1930

Answers (1)

Grigory
Grigory

Reputation: 1024

Posting the code here suggested me adding the @SkipValidation annotation to the add() method. And it started to execute. As Tom suggested I really was hitting an validation error which was not output to the errors section. And this preveneted the method from being executed.

Upvotes: 1

Related Questions