Skip to content

Instantly share code, notes, and snippets.

@stevehanson
Created January 10, 2014 15:16
Show Gist options
  • Save stevehanson/8356172 to your computer and use it in GitHub Desktop.
Save stevehanson/8356172 to your computer and use it in GitHub Desktop.
Spring MVC Example
<!-- src/main/webapp/WEB-INF/view/home.jsp -->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!doctype html>
<html>
<head>
<title>Spring Tutorial</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>Hello ${name}!</h1>
<form:form action="/spring-web2/submit" modelAttribute="person">
<form:input type="text" path="name" class="form-ctrl" placeholder="Enter name"/>
<form:input type="text" path="age" class="form-ctrl" placeholder="Enter age"/>
<input type="submit" value="submit" class="btn btn-primary" />
</form:form>
Hello ${person.name}, age ${person.age}
</body>
</html>
// com.tutorial.MainController.java
package com.tutorial;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@RequestMapping("/")
public String hello(Model m) {
m.addAttribute("person", new Person());
return "home";
}
@RequestMapping("/hello")
public String hellosdf(Model m) {
m.addAttribute("name", "Rene");
return "home";
}
@RequestMapping(value="/hello", params={"name"})
public String helloWithParam(@RequestParam("name") String name, Model m) {
System.out.println(name);
m.addAttribute("name", name);
return "home";
}
@RequestMapping(value="/hello/{name}")
public String helloWithPath(@PathVariable("name") String name, HttpServletRequest req, HttpServletResponse res, Model m) {
System.out.println(name);
m.addAttribute("name", name);
return "home";
}
@RequestMapping(value="/response")
public void helloWithPath(HttpServletResponse res) throws IOException {
res.getWriter().write("Hello world");
}
@RequestMapping(value="/json")
@ResponseBody
public Person json() {
return new Person();
}
@RequestMapping(value="/submit", method=RequestMethod.POST)
public String handleForm(@ModelAttribute Person person, Model m) {
m.addAttribute("person", person);
return "home";
}
}
<!-- src/main/webapp/WEB-INF/spring/sample-servlet.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.tutorial" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- src/main/webapp/WEB-INF/web.xml -->
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/sample-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment