Inversion of Control [sec 4-6]
(spring-demo-one) The approach of outsourcing the construction and management of objects. In IoC, actual implementation can be service locator or dependency injection.
- add lib files (spring + commons-logging-1.2.jar).
- applicationContext.xml to add beans.
- in main java, fetch context (of applicationContext.xml), then get bean (where the interface and instance class are specified).
Coach theCoach = context.getBean("myCoach", Coach.class);
- Dependency injection: specify dependency (object) by the caller instead of the callee instance. Injection can be done on constructor (in constructor-arg), setter(in property), or literal value (as value instead of ref).
- Bean scope:
- singleton: default scope; single shared instance of bean
- prototype: new bean for each container request
- request: scoped to HTTP web request
- session: scoped to HTTP web session
- global-session: scoped to global HTTP web session
- Life cycle of bean: (Spring does not call pre-destroy callback for prototype beans)
- loading context.xml
- construct instances
- injection of dependecies
- call post-construct (@PostConstruct, or init-method)
- retrieve bean
- call pre-destroy (@PreDestroy or destroy-method)
- close context.
Annotation [sec 7-9]
- enable component scan:
<context:component-scan base-package="com.@#$" />
- At the class definition, add @Component(“bean_alias”) (the default name for class AbcDef is abcDef)
- Autowired: after adding @Autowired, automatically fetch a bean (for constructor, method and field).
- If several beans match for autowired, @Qualifier(“bean_alias”) can specify which bean to use (for constructor, syntax is different as:
@Autowired public TennisCoach(@Qualifier("randomFortuneService") FortuneService theFortuneService)
- To inject properties file using Java annotation, we need to:
- load properties file in XML:
- write annotation as @Value(“${foo.email}”)
- load properties file in XML:
- Scope: At the class definition, add @Scope(“scope_type”)
Configuration by Java Code [sec 10]
- Create a config class, add @Configuration (optional: and @ComponentScan(“com.@#$”)).
- in main java, add
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(config.class);
- Add bean: in config class, add @Bean beanClass().
- Add properties file: in config class, add @PropertySource(“classpath:property”) (where classpath is src/).
Spring MVC [sec 11-13]
Client -> Dispatcher Servlet (mostly developed by Spring) -> (Model) -> Controller -> (Model) -> View -> Client
- add lib files (spring + commons-logging-1.2.jar + javax.servlet.jsp.jstl + javax.servlet.jsp.jstl.api).
- in WEB_INF, add web.xml and config.xml
- in web.xml:
<!-- Step 1: Configure Spring MVC Dispatcher Servlet --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
- in config.xml
<!-- Step 3: Add support for component scanning --> <context:component-scan base-package="com.luv2code.springdemo" /> <!-- Step 4: Add support for conversion, formatting and validation support --> <mvc:annotation-driven/> <!-- Step 5: Define Spring MVC view resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean>
- in web.xml:
- to add controller: create a controller class, add @Controller, add method like:
@RequestMapping("/") public String showPage() { return "main-menu"; }
- in jsp page, access variable (in get URL) as ${param.var}.
- To add model after controller:
@RequestMapping("/processForm") public String letsShout(HttpServletRequest request, Model model) { String name = request.getParameter("studentName"); // from GET URL name = name.toUpperCase(); model.addAttribute("message", name); return "helloworld"; }
Then to fetch data in jsp: ${message} (which is not shown in URL).
- To access static resources (CSS, img):
- in Spring config file (config.xml), add:
<mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>
- in view page, access as:
<img src="${pageContext.request.contextPath}/resources/images/spring-logo.png">
- in Spring config file (config.xml), add:
- To access param in request with annotation, change the method signature as:
@RequestMapping("/processForm") public String letsShout(@RequestParam("studentName") String theName, Model model) {
- Add hierarchical mapping: add @RequestMapping(“/parent-mapping”) in class name.
Form Tag and Data Binding [sec 14]
- in jsp, create form like:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
in header, and
<form:form action="processForm" modelAttribute="customer"> First name: <form:input path="firstName" /> ...
- in controller:
@RequestMapping("/processForm") public String processForm(@ModelAttribute("customer") Customer theCustomer, Model model) {
- Field is set on submit (i.e. setFirstName()). Then, model can even be passed to view page, which is accessed by ${customer.firstName}
- Drop-down list is
<form:select path="">
and options are
<form:option value="code" label="display" />
- Drop-down list can be populated with LinkedHashMap (or can be any Collection? not sure) in model:
- in jsp,
<form:options items="${customer.countryOptions}" />
- in model class, add getCountryOptions()
- in jsp,
- Radio button is
<form:radiobutton path="" value="" />
Radio button can also be populated in similar way as drop-down list.
- Check box is
<form:checkbox path="operatingSystems" value="" />
Check box value in java is array. To access check box in jsp, add
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
in header, and
<ul> <c: forEach var="temp" items="${customer.operatingSystems}"> <li> ${temp} </li> </c:forEach> </ul>