在Map中的null值

huangapple go评论35阅读模式
英文:

null value in Map<String,String>

问题

我有一个关于根据以下代码设置地图的问题。以这种方式,参数的值从用户那里接收,但在根据相关键输入值后,会打印出null。
请指导我。

public class ImportBatchCardRespServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private FileOutputStream out;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // Path path = Paths.get("F:\\");
            System.out.println("ImportBatchCardRespServlet-AccessFilesPath: " + getInitParameter("AccessFilesPath")); // vahid-log
            Path path = Paths.get(getInitParameter("AccessFilesPath"));

            // List<FileItem> items = new ServletFileUpload(new
            // DiskFileItemFactory()).parseRequest(request);
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory())
                    .parseRequest(new ServletRequestContext(req));
            Map<String, String> formFields = new HashMap<>();

            for (FileItem item : items) {
                if (item.isFormField()) {
                    // Process regular form field (input type="text|radio|checkbox|etc", select,
                    // etc).
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString();
                    System.out.println("req-fieldName: " + fieldName + ", req-fieldValue: " + fieldValue); // vahid-log
                    formFields.put(fieldName, fieldValue);
                    System.out.println("map-fieldName: " + formFields.get(fieldName) + ", map-fieldValue: " + formFields.get(fieldValue)); // vahid-log

如果还有其他需要翻译的部分,请提供具体内容。

英文:

I have a problem with setting the map according to the following code. In this way, the value of the parameters is received from the user, but after entering the value in the map according to the relevant key, null is printed.
please guide me

public class ImportBatchCardRespServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private FileOutputStream out;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			// Path path = Paths.get(&quot;F:\\&quot;);
			System.out.println(&quot;ImportBatchCardRespServlet-AccessFilesPath: &quot; + getInitParameter(&quot;AccessFilesPath&quot;)); // vahid-log
			Path path = Paths.get(getInitParameter(&quot;AccessFilesPath&quot;));

			// List&lt;FileItem&gt; items = new ServletFileUpload(new
			// DiskFileItemFactory()).parseRequest(request);
			List&lt;FileItem&gt; items = new ServletFileUpload(new DiskFileItemFactory())
					.parseRequest(new ServletRequestContext(req));
			Map&lt;String,String&gt; formFields = new HashMap&lt;&gt;();
			
			for (FileItem item : items) {
				if (item.isFormField()) {
					// Process regular form field (input type=&quot;text|radio|checkbox|etc&quot;, select,
					// etc).
					String fieldName = item.getFieldName();
					String fieldValue = item.getString();
					System.out.println(&quot;req-fieldName: &quot;+fieldName+&quot;,req-fieldValue: &quot;+fieldValue); //vahid-log
					formFields.put(fieldName,fieldValue);
					System.out.println(&quot;map-fieldName: &quot;+formFields.get(fieldName)+&quot;,map-fieldValue: &quot;+formFields.get(fieldValue)); //vahid-log

答案1

得分: 1

formFields.put(fieldName, fieldValue);

将一个键/值对添加到您的HashMap中,其中"fieldName"是键,"fieldValue"是值。
在下一行...

System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log

...看起来你想要测试打印键/值对。
然而,HashMap的"get"方法的参数是键/值对的,因此

formFields.get(fieldName)

将返回键/值对的(即fieldValue),而

formFields.get(fieldValue)

可能会返回null,因为没有与值相同的键。
正确的输出应该使用以下检查行可用:

System.out.println("map-fieldName: "+fieldName+",map-fieldValue: "+formFields.get(fieldName)); //vahid-log

干杯!

英文:

formFields.put(fieldName,fieldValue);

adds a value/key pair to your Hashmap, with "fieldName" being the key and "fieldValue" the value.
In the following line...

System.out.println(&quot;map-fieldName: &quot;+formFields.get(fieldName)+&quot;,map-fieldValue: &quot;+formFields.get(fieldValue)); //vahid-log

... it seems you want to test-print the key/value pair.
However, HashMap's "get" method parameter is the key of the key/value pair, therefore

formFields.get(fieldName)

will return the value of the key/value pair (i.e. fieldValue) and

formFields.get(fieldValue)

will likely return null as there is no key with the same name like the value.
The correct output should be available using this check line instead:

System.out.println(&quot;map-fieldName: &quot;+fieldName+&quot;,map-fieldValue: &quot;+formFields.get(fieldName)); //vahid-log

Cheers!

huangapple
  • 本文由 发表于 2020年7月29日 22:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63156054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定