java - struts2 操作类中的方法未执行

标签 java struts2

我正在将一个 web 应用程序从 struts 2.0 移植到 2.3。我无法使 DynamicMethodInitation 工作。 我有一个带有 add() 方法的操作类。 register!add.shtml 上提交表单不会执行此方法。 我找不到任何理由。

这是我使用的 jar :

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

最佳答案

在此处发布代码建议我将 @SkipValidation 注释添加到 add() 方法中。它开始执行。正如汤姆建议的那样,我确实遇到了验证错误,该错误没有输出到错误部分。这阻止了该方法的执行。

关于java - struts2 操作类中的方法未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937358/

相关文章:

java - 二叉树的直径 - 更好的设计

java - 物理模拟,将像素调整为米?

java - 基于 Web 的应用程序中的删除确认

java - Struts2 文本字段中的 Onchange

java - 使用 hibernate 插入数据库但出现 java.lang.reflect.InitationTargetException 异常

java - Azure 移动服务 Android 错误

java - 如何同时响应两个不同的 KeyEvents?

java - 在 Spring AbstractRoutingDataSource 中访问 Struts2 语言环境

java - 如何使用 Struts 2 确定页面生成时间?

java - 在 Struts2 拦截器中使用资源包