2013年10月31日星期四

struts2 transfer map questions ?

Action code is as follows :
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller("testAction")
@Scope("prototype")
public class TestAction extends BaseAction {
private static Log log = LogFactory.getLog(TestAction.class);
private Map<String, String> map;

public String init() {
return "init";
}

public String test() {

System.out.println(map);

return null;
}

public void setMap(Map<String, String> map) {
this.map = map;
}

}


struts.xml file as follows:
<action name="test-*" class="testAction" method="{1}">
<result name="init">/WEB-INF/page/test.jsp</result>
</action>


simple jsp test page as follows:
<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp" %>
<html>
  <head>
    <title>测试页面</title>
  </head>
  <body>
  <div class="container-fluid">
   <s:form action="test-test.jhtml" method="post" namespace="/" id="form_id">
   <input type="text" name="map['a90']" value='A' />
   <input type="text" name="map['a91']" value='A' />
   <input type="text" name="map['a92']" value='A' />
   <button type="submit" class="btn btn-success">提交</button>
   </s:form>
  </div>
  </body>
</html>


Description : backstage pass to the map , the result is only one value , as shown
strange here , I passed on the page three values ​​, and finally into only one value .

for a long time , and finally found in the Action , provided only setMap way , did not provide a method getMap caused , to ask you more proficient struts2 great God , I previously understood , struts2 properties when injected into the background , only need to provide set * methods can , why pass map, get method must provide it ?



------ Solution ------------------------------------ --------
I think this process
You say to map values ​​when injected inside
is the front desk directly next pass a map encapsulated type

or pass next station is three values
Then one by one assignment to the map inside it ?

I think it should be the second.
then add a numerical process should first obtain a map object, if not , then you get new one , and then assign the map.

this speculation , not necessarily accurate, because I was lazy , I did not read source
------ Solution -------------------- ------------------------
upstairs speculation is right. This problem is most clearly see the source code . I'm still looking for a better understanding of the paste over , it should be able to understand :

http://www.360doc.com/content/11/0406/20 / 1947337_107663126.shtml
------ Solution -------------------------------- ------------
Logically getMap will perform three setMap this and if you do not write will be executed once each time getMap will regenerate Map and perform setMap method, so your final result is a map on the inside is the last one value and the value

没有评论:

发表评论