簡體   English   中英

Struts2:不調用Action的屬性設置器

[英]Struts2 : Action's property setter is not invoked

我嘗試發布一個簡單的表格:

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

該操作將輸出寄存器字符串,但不會調用設置器。

為什么永不叫二傳手? 即使當我在瀏覽器http://localhost:8686/register.action?lastname=sdjh直接鍵入操作時,設置程序也不會被調用,但是execute方法會輸出“ register”。

在您的代碼中沒有看到任何遺漏的部分。 我試圖復制相同的東西,但發現它運行良好。 以下是我的代碼。

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

一個.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM