Last modified by Ken McWilliams on 2012/03/11 06:21

From version Icon 2.2 Icon
edited by Ken McWilliams
on 2012/03/10 19:49
Change comment: There is no comment for this version
To version Icon 2.3 Icon
edited by Ken McWilliams
on 2012/03/10 20:01
Change comment: There is no comment for this version

Summary

Details

Icon Page properties
Content
... ... @@ -83,7 +83,63 @@
83 83  
84 84  Our example will require the following things: 1) A service, 2) an implementation of the service, 3) configure spring to inject our service where we need it, 4) an action to use the service and 5) a view.
85 85  
86 -Create the class **SpringTest** under the namespace com.yournamespace.action.test where com.yournamespace can be anything:
86 +=== Create Service ===
87 87  
88 +package com.example.project.service;
89 +\\public interface Test {
90 + String doTest();
91 +}
88 88  
89 -4) Bring Spring up to the latest version
93 +=== Create Service Implementation ===
94 +
95 +package com.example.project.impl;
96 +import com.example.project.service.Test;
97 +\\public class TestImpl implements Test{
98 + @Override
99 + public String doTest() {
100 + return "From test.";
101 + }
102 +}
103 +
104 +=== Configure Spring to Inject the Service ===
105 +
106 +In applicationContext.xml add the following on the line before </beans>:
107 +
108 +<bean id="test" class="com.example.project.impl.TestImpl"/>
109 +
110 +=== Create an Action to use the Bean ===
111 +
112 +package com.example.project.action.test;
113 +\\import com.example.project.service.Test;
114 +import com.opensymphony.xwork2.ActionSupport;
115 +import org.springframework.beans.factory.annotation.Autowired;
116 +\\public class SpringTest extends ActionSupport{
117 + @Autowired Test test;
118 + public String output;
119 + @Override
120 + public String execute(){
121 + output = test.doTest();
122 + return SUCCESS;
123 + }
124 +}
125 +
126 +
127 +=== The View ===
128 +
129 +Create the JSP// /WEB-INF/content/test/spring-test.jsp//
130 +
131 +<%@taglib prefix="s" uri="/struts-tags"%>
132 +<%@page contentType="text/html" pageEncoding="UTF-8"%>
133 +<!DOCTYPE html>
134 +<html>
135 + <head>
136 + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
137 + <title>Spring Test</title>
138 + </head>
139 + <body>
140 + <h1>Spring Test</h1>
141 + Value from spring bean: <s:property value="output"/>
142 + </body>
143 +</html>
144 +
145 +Run the example... and append test/spring-test to the end of your application path and you should see the result.