JHU Object-Oriented Software Engineering Course Homepage
Using Form Beans
Table of Contents

Persistence


11. Creating Service Beans

Now that our application can receive requests and reply to them, we will add some application logic. We will modify this application to require the user to guess some unknown name; the user will be redirected back to the guess page on failure but will be directed to some other page on success. As discussed before, we keep application logic in a different tier than presentation logic. In the testApp project, create a new package named edu.jhu.cs.oose.j2ee.example.service. In that package, create a new interface called GuessService and add to it a method which allows a user to guess the secret name.

Then, create a corresponding class GuessServiceImpl which implements the GuessService interface. You can use the following code to do so:

@Service public class GuessServiceImpl implements GuessService { private static final String ANSWER = "Rumplestiltskin"; public boolean guessName(String guess) { return guess.equalsIgnoreCase(ANSWER); } }
Warning: Make sure you import org.springframework.stereotype.Service and not any other Service type.
Warning: It is necessary that the interface and the implementation be separate; we cannot skip creating the interface and wire directly to the class. Several of the libraries we are using will rely on the proxy design pattern to accomplish their work; this only works if an interface layer is present.

Now, add a new field to the TestController to represent the new service you created. We will make use of the Spring Autowired annotation to ensure that Spring will connect the TestController to the GuessService implementation; neither of these two classes need to know about the other.

@Autowired private GuessService guessService;

We will also want to be able to inform the user if his or her guess is incorrect. Add a boolean field named wrong to the TestBean and give it an appropriate getter and setter.

Next, modify the post method of the TestController to call the guess service to determine if the guess was correct. If it was correct, we will send the user to a different JSP; otherwise, we set the wrong flag. By the time you are finished, your class will look like this:

@Controller @RequestMapping(value = "/welcome") public class TestController { @Autowired private GuessService guessService; private TestBean testBean; public TestController() { this.testBean = new TestBean(); } @RequestMapping(method = RequestMethod.GET) public ModelAndView get() { ModelAndView model = new ModelAndView("test"); model.addObject(this.testBean); return model; } @RequestMapping(method = RequestMethod.POST) public ModelAndView post(@ModelAttribute("testBean") TestBean testBean) { if (this.guessService.guessName(testBean.getName())) { return new ModelAndView("correct"); } else { ModelAndView model = new ModelAndView("test"); model.addObject(testBean); testBean.setWrong(true); return model; } } }

We must alter the test.jsp so that it makes use of the property we just added to TestBean. Change your test.jsp to include the following at the beginning of the body.

<c:if test="${testBean.wrong}"> <p><b>Wrong!</b></p> </c:if>

The above represents dynamic content. The contents of the test attribute are an EL expression; in this case, the c:if tag only renders its contents if the user's guess was wrong.

Finally, we will create a page to represent success. Create a simple JSP in the WebContent/WEB-INF/pages directory called correct.jsp that congratulates the player on winning the game.

Republish your application by right-clicking the server and choosing "clean". When you visit the page and submit an incorrect guess, you should see something like the following:

When you guess correctly, you instead receive your success page (correct.jsp).

With an application logic layer in place, your application can now grow to include a variety of functionality over a number of pages. Next, we will discuss using your application's database for persistent storage.


Using Form Beans
Table of Contents

Persistence

Written by Zachary Palmer with help from Varun Sharma. Corrections and suggestions are welcome; please e-mail zachary dot palmer xX att Xx jhu.edu