Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you haven't done so already, check out the Spring MVC step-by-step tutorial.</p> <p><a href="http://static.springsource.org/docs/Spring-MVC-step-by-step/" rel="nofollow noreferrer">Spring MVC tutorial</a></p> <p>This will give you an excellent example of how to construct a simple Spring MVC web application. </p> <p>The only problem is it doesn't cover using Annotations in Spring 2.5+. You'll have to either wait for the Spring 3.0 MVC tutorial to release, or dig through the official Spring documentation yourself to learn how to use it. </p> <p><strong>EDIT</strong></p> <p>Here's an example involving controlling a form:</p> <p>Note: Spring 3.0.0</p> <p>Your controller:</p> <pre><code>@Controller @RequestMapping("/blog.htm") public class BlogController{ // Service layer class private final BlogManager blogManager; // Inject the blog manager into the constructor automatically @Autowired public BlogController(BlogManager blogManager){ this.blogManager = blogManager } // Set up the form and return the logical view name @RequestMapping(method=RequestMethod.GET) public String setupForm(ModelMap model){ model.addAttribute(new EntryForm()); return "addBlog"; } // Executed when posting the form @RequestMapping(method=RequestMethod.POST) public String addBlog(@ModelAttribute("entryForm")EntryForm entryForm){ // Read the entry form command object from the form String text = entryForm.getText(); // Call your service method blogManager.addEntry(text); // Usually redirect to a new logical view name return "redirect:/homepage"; } } </code></pre> <p>Form command object:</p> <pre><code>public class EntryForm{ private String text; // setters and getters for text } </code></pre> <p>Here is your service layer class</p> <pre><code>public class BlogManager{ private final BlogDAO blogDAO; @Autowired public BlogManager(BlogDAO blogDAO){ this.blogDAO = blogDAO; } public void addEntry(String text){ int blogID = 100; // simple example id for a blog Blog blog = blogDAO.findById(blogID); blog.addEntry(text); blogDAO.update(blog); } } </code></pre> <p>And now you update your spring.xml file to tie it all together</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt; &lt;!-- Scan for spring annotations --&gt; &lt;context:component-scan base-package="test.package"/&gt; &lt;!-- defined in the xml file and autowired into controllers and services --&gt; &lt;bean id="blogManager" class="test.package.BlogManager" /&gt; &lt;bean id="blogDAO" class="test.package.dao.BlogDAO" /&gt; &lt;!-- Resolves logical view names to jsps in /WEB-INF/jsp folder --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /property&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"&gt;&lt;/property&gt; &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload