Struts2:不调用Action的属性setter

时间:2017-01-15 12:43:20

标签: java struts2 action

我尝试发布一个简单的表单:

<s:form action="register">
<s:textfield name="lastname"></s:textfield>
    <s:submit></s:submit>
</s:form>

行动:

public class RegisterAction extends ActionSupport {
    private String lastname;
    public String execute() {
        System.out.println("register");
    }

    public String getLastname() {
      return lastname;
    }

    public void setLastname(String lastname) {
      this.lastname = lastname;
    }
}

动作输出寄存器字符串,但永远不会调用setter。

为什么永远不会调用setter? 即使我在浏览器http://localhost:8686/register.action?lastname=sdjh中直接键入操作,也不会调用setter,但execute方法会输出“register”。

1 个答案:

答案 0 :(得分:0)

我没有在代码中看到任何遗漏的部分。我试图复制它,我发现它工作得很好。以下是我的代码。

<强>的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

<强> 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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <action name="register" class="com.tmp.RegisterAction">
            <result>one.jsp</result>
        </action>

    </package>

</struts>

<强>的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=UTF-8">
</head>
<body>
    <s:form action="register">
        <s:textfield name="lastname"></s:textfield>
        <s:submit></s:submit>
    </s:form>
</body>
</html>

<强> RegisterAction

    package com.tmp;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionSupport;


    public class RegisterAction extends ActionSupport {

        private static final long   serialVersionUID    = 1L;

        String                      lastname;


        public String getLastname() {

            return lastname;
        }


        public void setLastname( String lastname ) {

            this.lastname = lastname;
        }


        @Override
        public String execute() throws Exception {

            // TODO Auto-generated method stub
            return Action.SUCCESS;
        }
    }

<强> one.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
</head>
<body>
<s:property value="lastname"/>
</body>
</html>