struts2 action类的方法没有执行

时间:2014-04-08 12:41:42

标签: java struts2

我将webapp从struts 2.0移植到2.3。我无法使DynamicMethodInvocation工作。 我有一个使用add()方法的动作类。 register!add.shtml上提交表单不会执行此方法。 我找不到任何理由。

以下是我使用的罐子:

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

这里是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>

还有一个简单的struts-beans.xml。没有其他文件,例如.properties

任何?

PS这里是动作代码。但我认为这不重要。我试过在第一行放一个断点。但是调试器并没有达到目的。

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;
}

1 个答案:

答案 0 :(得分:1)

在此处发布代码建议我将@SkipValidation注释添加到add()方法。它开始执行了。正如汤姆所说,我确实遇到了一个未输出到错误部分的验证错误。这使得该方法不再被执行。