Skip to content

Commit

Permalink
Implement Post-Redirect-Get
Browse files Browse the repository at this point in the history
Note that CSRF protection doesn't work yet, see
mvc-spec/ozark#202
  • Loading branch information
mthmulders committed Sep 6, 2018
1 parent 8b5e1a8 commit f8900f5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,54 @@
import javax.mvc.Models;
import javax.mvc.Controller;
import javax.mvc.View;
import javax.mvc.security.CsrfProtected;
import javax.ws.rs.GET;
import javax.ws.rs.FormParam;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.QueryParam;
import java.util.Locale;

@RequestScoped
@Controller
@Path("hello")
public class HelloController {
@Inject
UserPreferences preferences;

@Inject
Models models;

@Inject
GreetingService greetingService;

@GET
@Path("/start")
@View("input.jsp")
public void start() {
}

private Locale detectLocale(final String input) {
if (input != null && input.length() > 0) {
return Locale.forLanguageTag(input);
} else {
return Locale.US;
}
}

@CsrfProtected
@POST
@Path("/configure")
@View("redirect:/hello")
public void configure(@FormParam("locale") final String locale) {
final Locale result = detectLocale(locale);
preferences.setLocale(result);
}

@GET
@View("hello.jsp")
public void hello(@QueryParam("name") final String name) {
models.put("greeting", greetingService.generateGreeting(name));
final Locale locale = preferences.getLocale();
models.put("greeting", greetingService.generateGreeting(locale, name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package it.mulders.junk.mvc.presentation;

import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Locale;

@SessionScoped
public class UserPreferences implements Serializable {
private Locale locale;

public Locale getLocale() {
return locale;
}

public void setLocale(Locale locale) {
this.locale = locale;
}
}
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/views/hello.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
</head>
<body>
<h1>Hello, ${greeting.name}</h1>
<div>Current date and time is ${greeting.currentDateTime}</div>
<div>Current date and time is: ${greeting.currentDateTime}</div>
</body>
</html>
22 changes: 22 additions & 0 deletions src/main/webapp/WEB-INF/views/input.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html>
<head>
<title>Hello MVC</title>
</head>
<body>
<div>
<form action="${pageContext.request.contextPath}/app/hello/configure" method="post">
<input type="hidden" name="${mvc.csrf.name}" value="${mvc.csrf.token}"/>
<label for="locale">Select the language of your choice</label>
<select id="locale" name="locale">
<option value="nl-NL">Dutch</option>
<option value="de-DE">German</option>
<option value="fr-FR">French</option>
<option value="en-UK">English (UK)</option>
<option value="en-US">English (US)</option>
</select>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>

0 comments on commit f8900f5

Please sign in to comment.