Spring

From Null-pointer

Jump to: navigation, search

Contents

annotations

  • @Controller = controller
  • @RequestMapping("/") = the url to map
  • @SessionAttributes("modelview") = the session value to look up
  • @ModelAttribute("modelview") Object obj = the session or request attribute to look up
  • @PathVariable("groupId") int groupId = convert the request mapping {value} to a variable
  • @RequestParam("param") String param = convert the query string "?name=value"
  • @RequestBody Object obj = convert marshalled object from request content, normally used with xml content
  • @ResponseBody = the return value is the response body rather than using the spring ModelAndView.

examples

  1. @Controller
  2. @RequestMapping("/myApp/group")
  3. @SessionAttributes("modelview")
  4. public class GroupController { /* class body */ }

This is a controller which handles base URL /myApp/group and looks for session attributes named "modelview"

  1. @ResponseBody
  2. @RequestMapping(value="/{groupId}/users/", method=RequestMethod.GET)
  3. public String getUsersOfGroup(@ModelAttribute("application") Application application,
  4.                       @PathVariable("apiVersion") String apiVersion,
  5.                       @PathVariable("groupId") int groupId,
  6.                       @PathVariable("userId") int userId,
  7.                       @RequestParam("limit") Integer limit,
  8.                       @RequestParam(value="offset", required=false) Integer offset,
  9.                       HttpServletResponse response) { /* method body */ } }}

context:component-scan

  • multi-packages

<context:component-scan base-package="bbc.iplayer.ace.rest,bbc.iplayer.ace.service.impl"/>

it looks for:

  • @Controller
  • @Repository
  • @Service
  • @Component

Data access using JDBC

NamedParameterJdbcTemplate

String insert = "INSERT INTO " + POJO.class + " ( `name` ) VALUES ( :name ) "; this.simpleJdbcTemplate.update( insert, new MapSqlParameterSource().addValue("name", name) );

See

Resource servlet

For loading static resources, even from within /WEB-INF/

web.xml

<web-app ....>
	<servlet>
		<servlet-name>XSL Servlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.ResourceServlet</servlet-class>
		<init-param>
			<param-name>contentType</param-name>
			<param-value>text/xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>Resource Servlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.ResourceServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>XSL Servlet</servlet-name>
		<url-pattern>/api/resource/xsl/*</url-pattern>
	</servlet-mapping>
 
	<servlet-mapping>
		<servlet-name>Resource Servlet</servlet-name>
		<url-pattern>/api/resource/*</url-pattern>
	</servlet-mapping>
</web-app>

includes.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="resourcePath" value="${pageContext.servletContext.contextPath}/api/resource?resource=" />

myFile.jsp

<%@include file="../includes.jsp" %>
<link rel="stylesheet" type="text/css" href="${resourcePath}/css/blackjack.css" />

See also

Testing

Context management and caching

package com.example; @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "classpath:/com/example/MyTest-context.xml" @ContextConfiguration public class MyTest { // class body... }

@RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml" // in the root of the classpath @ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"}) public class MyTest { // class body... }

@RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "/base-context.xml" in the root of the classpath @ContextConfiguration(locations={"/base-context.xml"}) public class BaseTest { // class body... } // ApplicationContext will be loaded from "/base-context.xml" and "/extended-context.xml" // in the root of the classpath @ContextConfiguration(locations={"/extended-context.xml"}) public class ExtendedTest extends BaseTest { // class body... }

See

Security

Bean defintions

How to statically define the bean objects for Maps, and List.

<bean id="myFilter" name="myFilter" class="null-pointer.filter.MyFilter">
		<property name="allowChainingOnException">
			<value>true</value>
		</property>
		<!-- a map which has a list for its value -->
		<property name="groupRoleAccess">
			<map>
				<entry key="Group1" value-ref="Group1Roles"/>
				<entry key="Group2" value-ref="Group2Roles"/>
			</map>
		</property>
 
		<!-- just a list -->
		<property name="groupsWithAnyRole">
			<list>
				<value>Group3</value>
				<value>Group4</value>
			</list>
		</property>
		<property name="rolesWithAnyGroup">
			<list>
				<value>Role4</value>
			</list>
		</property>
	</bean>
 
	<!-- my list definitions -->
 
	<util:list id="Group1Roles">
		<value>Role1</value>
		<value>Role2</value>
	</util:list>
	<util:list id="Group2Roles">
		<value>Role2</value>
		<value>Role3</value>
	</util:list>

See also

Personal tools