1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.util;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.struts2.views.util.UrlHelper;
27
28
29 /***
30 * A bean that can generate a URL.
31 *
32 */
33 public class URLBean {
34
35 HashMap params;
36 HttpServletRequest request;
37 HttpServletResponse response;
38 String page;
39
40
41 public void setPage(String page) {
42 this.page = page;
43 }
44
45 public void setRequest(HttpServletRequest request) {
46 this.request = request;
47 }
48
49 public void setResponse(HttpServletResponse response) {
50 this.response = response;
51 }
52
53 public String getURL() {
54
55 Map fullParams = null;
56
57 if (params != null) {
58 fullParams = new HashMap();
59 }
60
61 if (page == null) {
62
63
64 if (fullParams != null) {
65 fullParams.putAll(request.getParameterMap());
66 } else {
67 fullParams = request.getParameterMap();
68 }
69 }
70
71
72 if (params != null) {
73 fullParams.putAll(params);
74 }
75
76 return UrlHelper.buildUrl(page, request, response, fullParams);
77 }
78
79 public URLBean addParameter(String name, Object value) {
80 if (params == null) {
81 params = new HashMap();
82 }
83
84 if (value == null) {
85 params.remove(name);
86 } else {
87 params.put(name, value.toString());
88 }
89
90 return this;
91 }
92
93 public String toString() {
94 return getURL();
95 }
96 }