-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpring MVC_demo.txt
209 lines (134 loc) · 4.69 KB
/
Spring MVC_demo.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
1.
Spring MVC
create a Maven project
select archetype webapp
group id
artifact id
ver
pacakage
-----------------------------------------
2.
JDK 8
Add proper Maven dependency at pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
-------------------------------------------------
3. set the target runtime to server
-----------------------------------------
4. create a java folder under src/main if required only
src/main/java
---------------------------------------------------
5. package com.niit.springwebexample.domain;
public interface HelloWorld {
public String sayHello();
}
-------------------------------------------
6.
package com.niit.springwebexample.domain;
import org.springframework.stereotype.Component;
@Component("helloWorld")
public class HelloWorldImpl implements HelloWorld{
public String sayHello() {
return "HelloWorld";
}
}
---------------------------------------------
7.
package com.niit.springwebexample.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.niit.springwebexample.domain.HelloWorld;
@Controller
public class HelloController {
@Autowired
private HelloWorld helloWorld; //DI
@GetMapping("/")
public String hello(Model model) {
model.addAttribute("hello", helloWorld.sayHello());
return "index"; // logical name for output is index
// model here is used for preparing the data
}
// every method is mapped to one url
}
-----------------------------------------------------
8.
config :-> 2 files.
package com.niit.springwebexample.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.niit.springwebexample.domain.HelloWorld;
import com.niit.springwebexample.domain.HelloWorldImpl;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.niit")
public class AppConfig {
@Bean(name="viewResolver")
public ViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
----------------------------------------------------
package com.niit.springwebexample.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] {AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
}
-----------------------------------------------
9.
Web-inf/views/
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ page session="false"%>
<html>
<body>
<h2>Hello World!</h2>
<h2>${hello}</h2>
</body>
</html>
----------------------------------------------------