|
| 1 | +:toc: |
| 2 | +:toclevels: 4 |
| 3 | + |
1 | 4 | == Rewrite Annotations |
2 | 5 |
|
| 6 | +toc::[] |
| 7 | + |
3 | 8 | The Rewrite annotation support offers an alternative way to configure Rewrite. Compared to configuring |
4 | 9 | Rewrite using a +ConfigurationProvider+, the annotations allow to place the configuration very close |
5 | 10 | to the affected code, which some people prefer over a central configuration. |
@@ -45,7 +50,7 @@ If you want to disable the annotation scanning completely, set the package to sc |
45 | 50 | By default Rewrite will only scan the contents of your +/WEB-INF/classes+ directory. |
46 | 51 | Sometimes it makes sense to also scan the JAR files located in |
47 | 52 | +/WEB-INF/lib+. This may be the case if your are developing a highly modular application |
48 | | -which is split into different JAR. |
| 53 | +which is split into different JAR files. |
49 | 54 |
|
50 | 55 | You can tell Rewrite to also scan the JAR files in +/WEB-INF/lib+ by adding the |
51 | 56 | following configuration to your +web.xml+: |
@@ -116,13 +121,103 @@ to do this. |
116 | 121 |
|
117 | 122 | === Handling parameters |
118 | 123 |
|
| 124 | +Parameters are a very important concept in Rewrite. They allow you to specify a _dynamic_ part of your |
| 125 | +URL used to identify the resource the user is trying to access. |
| 126 | + |
| 127 | +When using the +ConfigurationProvider+ API, you declare parameters like this: |
| 128 | + |
| 129 | +[source,java] |
| 130 | +.A join with parameters |
| 131 | +---- |
| 132 | +.addRule(Join.path("/user/{name}").to("/faces/user-details.xhtml")) |
| 133 | +---- |
| 134 | + |
| 135 | +If you want to define such a rule using Rewrite annotations, add the parameter |
| 136 | +to the path just like you would using the +ConfigurationProvider+: |
| 137 | + |
| 138 | +[source,java] |
| 139 | +.Using parameters with @Join annotations |
| 140 | +---- |
| 141 | +@Named |
| 142 | +@RequestScoped |
| 143 | +@Join(path="/user/{name}", to="/faces/user-details.xhtml"") |
| 144 | +public class UserDetailsPage { |
| 145 | + // your code |
| 146 | +} |
| 147 | +---- |
| 148 | + |
| 149 | +Rewrite will automatically turn the parameter into a query parameter with the same name. So |
| 150 | +you can access the value simply by calling +HttpServletRequest.getParameter()+: |
| 151 | + |
| 152 | +[source,java] |
| 153 | +.Accessing parameters using the Servlet API |
| 154 | +---- |
| 155 | +String username = request.getParameter("name"); |
| 156 | +---- |
| 157 | + |
| 158 | +Of cause it is not very nice to spread such low level Servlet API calls across your code. |
| 159 | +Therefore Rewrite also supports _parameter bindings_ which allow you to automatically |
| 160 | +inject the parameter values into managed bean properties. |
| 161 | + |
| 162 | +To inject the parameters into a property of you bean, just add a +@Parameter+ annotation |
| 163 | +to the corresponding field like this: |
| 164 | + |
| 165 | +[source,java] |
| 166 | +.Using @Parameter to inject parameter values |
| 167 | +---- |
| 168 | +@Named |
| 169 | +@RequestScoped |
| 170 | +@Join(path="/user/{name}", to="/faces/user-details.xhtml"") |
| 171 | +public class UserListPage { |
| 172 | +
|
| 173 | + @Parameter("name") |
| 174 | + private String username; |
| 175 | +
|
| 176 | +} |
| 177 | +---- |
| 178 | + |
| 179 | +TIP: If you don't specify the name of the parameter in the annotation, Rewrite will instead |
| 180 | +use the name of the field. So you don't have to specify the name if the name of the field |
| 181 | +matches the name of the parameter. |
| 182 | + |
| 183 | + |
| 184 | +==== Custom matching patterns |
| 185 | + |
| 186 | +By default Rewrite parameters will match any character except for the path delimiter +/+. |
| 187 | +Internally Rewrite uses the regular expression +[^/]++ for matching the value of the parameter. |
| 188 | +In some situations it can be useful to customize this pattern. Especially if the standard |
| 189 | +pattern matches too greedy or if you want to match more than a single path segment. |
| 190 | + |
| 191 | +If you want to customize the regular expression used for the matching, just add a +@Matches+ |
| 192 | +annotation to the corresponding field. The following class contains a parameter that will |
| 193 | +match only lowercase letters and digits. |
| 194 | + |
| 195 | +[source,java] |
| 196 | +.Using @Parameter to inject parameter values |
| 197 | +---- |
| 198 | +@Named |
| 199 | +@RequestScoped |
| 200 | +@Join(path="/user/{name}", to="/faces/user-details.xhtml"") |
| 201 | +public class UserListPage { |
| 202 | +
|
| 203 | + @Parameter |
| 204 | + @Matches("[a-z0-9]+") |
| 205 | + private String name; |
| 206 | +
|
| 207 | +} |
| 208 | +---- |
| 209 | + |
| 210 | +==== Custom validation |
| 211 | + |
119 | 212 | TODO |
120 | 213 |
|
| 214 | + |
121 | 215 | === Invoking methods |
122 | 216 |
|
123 | 217 | TODO |
124 | 218 |
|
125 | 219 | [[custom-annotations]] |
| 220 | + |
126 | 221 | === Building custom annotations |
127 | 222 |
|
128 | 223 | TODO |
0 commit comments