You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-spring.asciidoc 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. ---
  2. title: Vaadin Spring Add-on
  3. order: 18
  4. layout: page
  5. ---
  6. [[advanced.spring]]
  7. = Vaadin Spring Add-on
  8. ((("Vaadin Spring", id="term.advanced.spring.springlong", range="startofrange")))
  9. ((("Spring", id="term.advanced.spring.spring", range="startofrange")))
  10. Vaadin Spring and Vaadin Spring Boot add-ons make it easier to use Spring in
  11. Vaadin applications. Vaadin Spring enables Spring dependency injection with
  12. custom UI and view providers, and provides three custom scopes:
  13. [classname]#UIScope#, [classname]#ViewScope#, and
  14. [classname]#VaadinSessionScope#.
  15. API documentation for add-ons is available at:
  16. * Vaadin Spring API at link:https://vaadin.com/api/vaadin-spring[vaadin.com/api/vaadin-spring]
  17. * Vaadin Spring Boot API at link:https://vaadin.com/api/vaadin-spring-boot[vaadin.com/api/vaadin-spring-boot]
  18. To learn more about Vaadin Spring, the
  19. link:https://vaadin.com/wiki/-/wiki/Main/Vaadin+Spring[Vaadin Spring Tutorial]
  20. gives a hands-on introduction. The source code of the Spring Tutorial demo is
  21. available for browsing or cloning at
  22. link:https://github.com/Vaadin/spring-tutorial[github.com/Vaadin/spring-tutorial].
  23. [[advanced.spring.spring]]
  24. == Spring Overview
  25. __Spring Framework__ is a Java application framework that provides many useful
  26. services for building applications, such as authentication, authorization, data
  27. access, messaging, testing, and so forth. In the Spring core, one of the central
  28. features is dependency injection, which accomplishes inversion of control for
  29. dependency management in managed beans. Other Spring features rely on it
  30. extensively. As such, Spring offers capabilities similar to CDI, but with
  31. integration with other Spring services. Spring is well-suited for applications
  32. where Vaadin provides the UI layer and Spring is used for other aspects of the
  33. application logic.
  34. __Spring Boot__ is a Spring application development tool that allows creating
  35. Spring applications easily. __Vaadin Spring Boot__ builds on Spring Boot to
  36. allow creating Vaadin Spring applications easily. It starts up a servlet,
  37. handles the configuration of the application context, registers a UI provider,
  38. and so forth.
  39. Regarding general Spring topics, we recommend the following Spring
  40. documentation:
  41. ifdef::web[]
  42. * link:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/[Spring
  43. Framework Reference Documentation].
  44. * link:http://projects.spring.io/spring-framework/[Spring Project]
  45. * link:https://vaadin.com/wiki/-/wiki/Main/Vaadin+Spring[Vaadin Spring Tutorial]
  46. endif::web[]
  47. [[advanced.spring.spring.injection]]
  48. === Dependency Injection
  49. __Dependency injection__ is a way to pass dependencies (service objects) to
  50. dependent objects (clients) by injecting them in member variables or initializer
  51. parameters, instead of managing the lifecycle in the clients or passing them
  52. explicitly as parameters. In Spring, injection of a service object to a client
  53. is configured with the [classname]#@Autowired# annotation.
  54. For a very typical example in a web application, you could have a user data
  55. object associated with a user session:
  56. [source, java]
  57. ----
  58. @SpringComponent
  59. @VaadinSessionScope
  60. public class User implements Serializable {
  61. private String name;
  62. public void setName(String name) {this.name = name;}
  63. public String getName() {return name;}
  64. }
  65. ----
  66. The [classname]#@SpringComponent# annotation allows for automatic detection of
  67. managed beans by Spring. (The annotation is exactly the same as the regular
  68. Spring [classname]#@Component#, but has been given an alias, because Vaadin has
  69. a [interfacename]#Component# interface, which can cause trouble.)
  70. Now, if we have a UI view that depends on user data, we could inject the data in
  71. the client as follows:
  72. [source, java]
  73. ----
  74. public class MainView extends CustomComponent implements View {
  75. User user;
  76. Label greeting = new Label();
  77. @Autowired
  78. public MainView(User user) {
  79. this.user = user;
  80. ...
  81. }
  82. ...
  83. @Override
  84. public void enter(ViewChangeEvent event) {
  85. // Then you can use the injected data
  86. // for some purpose
  87. greeting.setValue("Hello, " + user.getName());
  88. }
  89. }
  90. ----
  91. Here, we injected the user object in the constructor. The user object would be
  92. created automatically when the view is created, with all such references
  93. referring to the same shared instance in the scope of the Vaadin user session.
  94. [[advanced.spring.spring.contexts]]
  95. === Contexts and Scopes
  96. __Contexts__ in Spring are services that manage the lifecycle of objects and
  97. handle their injection. Generally speaking, a context is a situation in which an
  98. instance is used with a unique identity. Such objects are essentially
  99. "singletons" in the context. While conventional singletons are application-wide,
  100. objects managed by a Spring container can be "singletons" in a more narrow
  101. __scope__: a user session, a particular UI instance associated with the session,
  102. a view within the UI, or even just a single request. Such a context defines the
  103. lifecycle of the object: its creation, use, and finally its destruction.
  104. Earlier, we introduced a user class defined as session-scoped:
  105. [source, java]
  106. ----
  107. @SpringComponent
  108. @VaadinSessionScope
  109. public class User {
  110. ----
  111. Now, when you need to refer to the user, you can use Spring injection to inject
  112. the session-scoped "singleton" to a member variable or a constructor parameter;
  113. the former in the following:
  114. [source, java]
  115. ----
  116. public class MainView extends CustomComponent implements View {
  117. @Autowired
  118. User user;
  119. Label greeting = new Label();
  120. ...
  121. @Override
  122. public void enter(ViewChangeEvent event) {
  123. greeting.setValue("Hello, " + user.getName());
  124. }
  125. }
  126. ----
  127. [[advanced.spring.boot]]
  128. == Quick Start with Vaadin Spring Boot
  129. The Vaadin Spring Boot is an add-on that allows for easily creating a project
  130. that uses Vaadin Spring. It is meant to be used together with Spring Boot, which
  131. enables general Spring functionalities in a web application.
  132. You can use the Spring Initializr at
  133. link:https://start.spring.io/[start.spring.io] website to generate a project,
  134. which you can then download as a package and import in your IDE. The generated
  135. project is a Spring application stub; you need to add at least Vaadin
  136. dependencies ( [literal]#++vaadin-spring-boot++#, [literal]#++vaadin-themes++#,
  137. and [literal]#++vaadin-client-compiled++#) and a UI class to the generated
  138. project, as well as a theme, and so forth.
  139. See the link:https://vaadin.com/wiki/-/wiki/Main/Vaadin+Spring[Vaadin Spring
  140. Tutorial] for detailed instructions for using Spring Boot.
  141. [[advanced.spring.installation]]
  142. == Installing Vaadin Spring Add-on
  143. Vaadin Spring requires a Java EE 7 compatible servlet container, such as
  144. Glassfish or Apache TomEE Web Profile, as mentioned for the reference toolchain
  145. in
  146. <<dummy/../../../framework/getting-started/getting-started-environment#getting-started.environment,"Setting
  147. up the Development Environment">>.
  148. To install the Vaadin Spring and/or Vaadin Spring Boot add-ons, either define
  149. them as an Ivy or Maven dependency or download from the Vaadin Directory add-on
  150. page at <<,vaadin.com/directory#addon/vaadin-spring>> or
  151. <<,vaadin.com/directory#addon/vaadin-spring-boot>>. See
  152. <<dummy/../../../framework/addons/addons-overview.asciidoc#addons.overview,"Using
  153. Vaadin Add-ons">> for general instructions for installing and using Vaadin
  154. add-ons.
  155. The Ivy dependency is as follows:
  156. [subs="normal"]
  157. ----
  158. &lt;dependency org="com.vaadin" name="vaadin-spring"
  159. rev="[replaceable]##latest.release##"/&gt;
  160. ----
  161. The Maven dependency is as follows:
  162. [subs="normal"]
  163. ----
  164. &lt;dependency&gt;
  165. &lt;groupId&gt;com.vaadin&lt;/groupId&gt;
  166. &lt;artifactId&gt;vaadin-spring&lt;/artifactId&gt;
  167. &lt;version&gt;[replaceable]##LATEST##&lt;/version&gt;
  168. &lt;/dependency&gt;
  169. ----
  170. [[advanced.spring.preparing]]
  171. == Preparing Application for Spring
  172. A Vaadin application that uses Spring must have a file named
  173. [filename]#applicationContext.xml# in the [filename]#WEB-INF# directory.
  174. [subs="verbatim,replacements,quotes"]
  175. ----
  176. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  177. &lt;beans xmlns="http://www.springframework.org/schema/beans"
  178. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179. xmlns:context="http://www.springframework.org/schema/context"
  180. xsi:schemaLocation="
  181. http://www.springframework.org/schema/beans
  182. http://www.springframework.org/schema/beans/spring-beans.xsd
  183. http://www.springframework.org/schema/context
  184. http://www.springframework.org/schema/context/spring-context-4.1.xsd"&gt;
  185. &lt;!-- Configuration object --&gt;
  186. &lt;bean class="[replaceable]#com.example.myapp.MySpringUI.MyConfiguration#" /&gt;
  187. &lt;!-- Location for automatically scanned beans --&gt;
  188. &lt;context:component-scan
  189. base-package="[replaceable]#com.example.myapp.domain#" /&gt;
  190. &lt;/beans&gt;
  191. ----
  192. The application should not have a servlet extending [classname]#VaadinServlet#,
  193. as Vaadin servlet has its own [classname]#VaadinSpringServlet# that is deployed
  194. automatically. If you need multiple servlets or need to customize the Vaadin
  195. Spring servlet, see <<advanced.spring.deployment>>.
  196. You can configure managed beans explicitly in the file, or configure them to be
  197. scanned using the annotations, which is the preferred way described in this
  198. section.
  199. [[advanced.spring.springui]]
  200. == Injecting a UI with [classname]#@SpringUI#
  201. ((("[classname]#@SpringUI#", id="term.advanced.spring.springui", range="startofrange")))
  202. Vaadin Spring offers an easier way to instantiate UIs and to define the URL
  203. mapping for them than the usual ways described in
  204. <<dummy/../../../framework/application/application-environment#application.environment,"Deploying
  205. an Application">>. It is also needed for enabling Spring features in the UI. To
  206. define a UI class that should be instantiated for a given URL, you simply need
  207. to annotate the class with [classname]#@SpringUI#. It takes an optional path as
  208. parameter.
  209. [source, java]
  210. ----
  211. @SpringUI(path="/myniceui")
  212. @Theme("valo")
  213. public class MyNiceUI extends UI {
  214. ...
  215. ----
  216. The path in the URL for accessing the UI consists of the context path of the
  217. application and the UI path, for example, [literal]#++/myapp/myniceui++#. Giving
  218. empty UI path maps the UI to the root of the application context, for example,
  219. [literal]#++/myapp++#.
  220. [source, java]
  221. ----
  222. @SpringUI
  223. ----
  224. See <<advanced.spring.deployment>> for how to handle servlet URL mapping of
  225. Spring UIs when working with multiple servlets in the same web application.
  226. (((range="endofrange", startref="term.advanced.spring.springui")))
  227. [[advanced.spring.scopes]]
  228. == Scopes
  229. ((("Spring", "scopes", id="term.advanced.spring.scopes", range="startofrange")))
  230. As in programming languages, where a variable name refers to a unique object
  231. within the scope of the variable, an object has unique identity within a scope
  232. in Spring. However, instead of identifying the objects by variable names, they
  233. are identified by their type (object class) and any qualifiers they may have.
  234. The scope of an object can be defined with an annotation to the class as
  235. follows:
  236. [source, java]
  237. ----
  238. @VaadinSessionScope
  239. public class User {
  240. ...
  241. ----
  242. Defining a scope in Spring is normally done with the [classname]#@Scope#
  243. annotation. For example, [literal]#++@Scope("prototype")++# creates a new
  244. instance every time one is requested/auto-wired. Such standard scopes can be
  245. used with some limitations. For example, Spring session and request scopes do
  246. not work in background threads and with certain push transport modes.
  247. Vaadin Spring provides three scopes useful in Vaadin applications: a session
  248. scope, a UI scope, a view scope, all defined in the
  249. [package]#com.vaadin.spring.annotation# package.
  250. [[advanced.spring.scopes.session]]
  251. === [classname]#@VaadinSessionScope#
  252. The session scope is the broadest of the custom scopes defined in Vaadin Spring.
  253. Objects in the Vaadin session scope are unique in a user session, and shared
  254. between all UIs open in the session. This is the most basic scope in Vaadin
  255. applications, useful for accessing data for the user associated with the
  256. session. It is also useful when updating UIs from a background thread, as in
  257. those cases the UI access is locked on the session and also data should be in
  258. that scope.
  259. [[advanced.spring.scopes.ui]]
  260. === [classname]#@UIScope#
  261. UI-scoped beans are uniquely identified within a UI instance, that is, a browser
  262. window or tab. The lifecycle of UI-scoped beans is bound between to the
  263. initialization and closing of a UI. Whenever you inject a bean, as long as you
  264. are within the same UI, you will get the same instance.
  265. Annotating a Spring view (annotated with [classname]#@SpringView# as described
  266. later) also as [classname]#@UIScoped# makes the view retain the same instance
  267. when the user navigates away and back to the view.
  268. [[advanced.spring.scopes.view]]
  269. === [classname]#@ViewScope#
  270. The annotation enables the view scope in a bean. The lifecycle of such a bean
  271. starts when the user navigates to a view referring to the object and ends when
  272. the user navigates out of the view (or when the UI is closed or expires).
  273. Views themselves are by default view-scoped, so a new instance is created every
  274. time the user navigates to the view.
  275. (((range="endofrange", startref="term.advanced.spring.scopes")))
  276. ifdef::web[]
  277. [[advanced.spring.navigation]]
  278. == View Navigation
  279. Vaadin Spring extends the navigation framework in Vaadin, described in
  280. <<dummy/../../../framework/advanced/advanced-navigator#advanced.navigator,"Navigating
  281. in an Application">>. It manages Spring views with a special view provider and
  282. enables view scoping.
  283. [[advanced.spring.navigation.ui]]
  284. === Preparing the UI
  285. You can define navigation for any single-component container, as described in
  286. <<dummy/../../../framework/advanced/advanced-navigator#advanced.navigator.navigating,"Setting
  287. Up for Navigation">>, but typically you set up navigation for the entire UI
  288. content. To use Vaadin Spring views, you need to inject a
  289. [classname]#SpringViewProvider# in the UI and add it as a provider for the
  290. navigator.
  291. [source, java]
  292. ----
  293. @SpringUI(path="/myspringui")
  294. public class MySpringUI extends UI {
  295. @Autowired
  296. SpringViewProvider viewProvider;
  297. @Override
  298. protected void init(VaadinRequest request) {
  299. Navigator navigator = new Navigator(this, this);
  300. navigator.addProvider(viewProvider);
  301. // Navigate to start view
  302. navigator.navigateTo("");
  303. }
  304. }
  305. ----
  306. [[advanced.spring.navigation.view]]
  307. === The View
  308. A view managed by Vaadin Spring only needs to have the [classname]#@SpringView#
  309. annotation, which registers the view in the [classname]#SpringViewProvider#. The
  310. annotation is also necessary to enable Spring features in the view, such as
  311. injection.
  312. [source, java]
  313. ----
  314. @SpringView(name=MainView.NAME)
  315. public class MainView extends CustomComponent implements View {
  316. public static final String NAME = "main";
  317. ...
  318. ----
  319. The annotation can have the following optional paramers:
  320. name (optional):: Specifies the path by which the view can be accessed programmatically and by the
  321. URI fragment.
  322. +
  323. [source, java]
  324. ----
  325. @SpringView(name="main")
  326. ----
  327. +
  328. If the view name is not given, it is derived from the class name by removing a
  329. possible "View" suffix, making it lower case, and using a dash ("-") to separate
  330. words originally denoted by capital letters. Thereby, a view class such as
  331. [classname]#MyFunnyView# would have name " [literal]#++my-funny++#".
  332. +
  333. It is a recommended pattern to have the view name in a static member constant in
  334. the view class, as was done in the example previously, so that the name can be
  335. referred to safely.
  336. supportsParameters:: Specifies whether view parameters can be passed to the view as a suffix to the
  337. name in the navigation state, that is, in the form of
  338. [literal]#++viewname+viewparameters++#. The view name is merely a prefix and
  339. there is no separator nor format for the parameters, but those are left for the
  340. view to handle. The parameter support mode is disabled by default.
  341. +
  342. [source, java]
  343. ----
  344. @SpringView(name="myview", supportsParameters=true)
  345. ----
  346. +
  347. You could then navigate to the state with a URI fragment such as
  348. [literal]#++#!myview/someparameter++# or programmatically with:
  349. +
  350. [source, java]
  351. ----
  352. getUI().getNavigator().navigateTo("myview/someparameter");
  353. ----
  354. +
  355. The [methodname]#enter()# method of the view gets the URI fragment as parameter
  356. as is and can interpret it in any application-defined way.
  357. +
  358. Note that in this mode, matching a navigation state to a view is done by the
  359. prefix of the fragment! Thereby, no other views may start with the name of the
  360. view as prefix. For example, if the view name is " [literal]#++main++#", you
  361. must not have a view named " [literal]#++maintenance++#".
  362. uis:: If the application has multiple UIs that use [classname]#SpringViewProvider#,
  363. you can use this parameter to specify which UIs can show the view.
  364. +
  365. [source, java]
  366. ----
  367. @SpringView(name="myview", uis={MySpringUI.class})
  368. ----
  369. +
  370. If the list contains [parameter]#UI.class#, the view is available to all UIs.
  371. +
  372. [source, java]
  373. ----
  374. @SpringView(name="myview", uis={UI.class})
  375. ----
  376. In the following, we have a login view that accesses a session-scoped user
  377. object. Here, we use a constant to define the view name, so that we can use the
  378. constant when navigating to it.
  379. [source, java]
  380. ----
  381. @SpringView(name=LoginView.NAME)
  382. public class LoginView extends CustomComponent
  383. implements View {
  384. public final static String NAME = "";
  385. // Here we inject to the constructor and actually do
  386. // not store the injected object to use it later
  387. @Autowired
  388. public LoginView(User user) {
  389. VerticalLayout layout = new VerticalLayout();
  390. // An input field for editing injected data
  391. BeanItem<User> item = new BeanItem<User>(user);
  392. TextField username = new TextField("User name",
  393. item.getItemProperty("name"));
  394. username.setNullRepresentation("");
  395. layout.addComponent(username);
  396. // Login button (authentication omitted) / Java 8
  397. layout.addComponent(new Button("Login", e ->
  398. getUI().getNavigator().
  399. navigateTo(MainView.VIEWNAME)));
  400. setCompositionRoot(layout);
  401. }
  402. @Override
  403. public void enter(ViewChangeEvent event) {}
  404. }
  405. ----
  406. You could now navigate to the view from any other view in the UI with:
  407. [source, java]
  408. ----
  409. getUI().getNavigator().navigateTo(LoginView.VIEWNAME);
  410. ----
  411. endif::web[]
  412. [[advanced.spring.accesscontrol]]
  413. == Access Control
  414. Access control for views can be implemented by registering beans implementing
  415. [interfacename]#ViewAccessControl# or
  416. [interfacename]#ViewInstanceAccessControl#, which can restrict access to the
  417. view either before or after a view instance is created.
  418. Integration with authorization solutions, such as Spring Security, is provided
  419. by additional unofficial add-ons on top of Vaadin Spring.
  420. [[advanced.spring.accesscontrol.accessdenied]]
  421. === Access Denied View
  422. By default, the view provider acts as if a denied view didn't exist. You can set
  423. up an "Access Denied" view that is shown if the access is denied with
  424. [methodname]#setAccessDeniedView()# in [classname]#SpringViewProvider#.
  425. [source, java]
  426. ----
  427. @Autowired
  428. SpringViewProvider viewProvider;
  429. @Override
  430. protected void init(VaadinRequest request) {
  431. Navigator navigator = new Navigator(this, this);
  432. navigator.addProvider(viewProvider);
  433. // Set up access denied view
  434. viewProvider.setAccessDeniedViewClass(
  435. MyAccessDeniedView.class);
  436. ----
  437. [[advanced.spring.deployment]]
  438. == Deploying Spring UIs and Servlets
  439. Vaadin Spring hooks into Vaadin framework by using a special
  440. [classname]#VaadinSpringServlet#. As described earlier, you do not need to map
  441. an URL path to a UI, as it is handled by Vaadin Spring. However, in the
  442. following, we go through some cases where you need to customize the servlet or
  443. use Spring with non-Spring servlets and UIs in a web application.
  444. [[advanced.spring.servlets.custom]]
  445. === Custom Servlets
  446. When customizing the Vaadin servlet, as outlined in
  447. <<dummy/../../../framework/application/application-lifecycle#application.lifecycle.servlet-service,"Vaadin
  448. Servlet, Portlet, and Service">>, you simply need to extend
  449. [classname]#com.vaadin.spring.internal.VaadinSpringServlet# instead of
  450. [classname]#com.vaadin.servlet.VaadinServlet#.
  451. [subs="normal"]
  452. ----
  453. @WebServlet(value = "[replaceable]#/*#", asyncSupported = true)
  454. public class [replaceable]#MySpringServlet# extends SpringVaadinServlet {
  455. }
  456. ----
  457. The custom servlet must not have [classname]#@VaadinServletConfiguration#, as
  458. you would normally with a Vaadin servlet, as described in
  459. <<dummy/../../../framework/application/application-environment#application.environment,"Deploying
  460. an Application">>.
  461. [[advanced.spring.deployment.urlmapping]]
  462. === Defining Servlet Root
  463. Spring UIs are managed by a Spring servlet ( [classname]#VaadinSpringServlet#),
  464. which is by default mapped to the root of the application context. For example,
  465. if the name of a Spring UI is " [literal]#++my-spring-ui++#" and application
  466. context is [literal]#++/myproject++#, the UI would by default have URL "
  467. [literal]#++/myproject/my-spring-ui++#". If you do not want to have the servlet
  468. mapped to context root, you can use a [classname]#@WebServlet# annotation for
  469. the servlet or a [filename]#web.xml# definition to map all Spring UIs to a
  470. sub-path.
  471. For example, if we have a root UI and another:
  472. [subs="normal"]
  473. ----
  474. @SpringUI(path=[replaceable]#""#) // At Spring servlet root
  475. public class [replaceable]#MySpringRootUI# extends UI {...}
  476. @SpringUI("[replaceable]#another#")
  477. public class [replaceable]#AnotherUI# extends UI {...}
  478. ----
  479. Then define a path for the servlet by defining a custom servlet:
  480. [subs="normal"]
  481. ----
  482. @WebServlet(value = "[replaceable]#/myspringuis/*#", asyncSupported = true)
  483. public class [replaceable]#MySpringServlet# extends SpringVaadinServlet {
  484. }
  485. ----
  486. These two UIs would have URLs /myproject/myspringuis and
  487. /myproject/myspringuis/another, respectively.
  488. You can also map the Spring servlet to another URL in servlet definition in
  489. [filename]#web.xml#, as described the following.
  490. [[advanced.spring.servlets.mixing]]
  491. === Mixing With Other Servlets
  492. The [classname]#VaadinSpringServlet# is normally used as the default servlet,
  493. but if you have other servlets in the application, such as for non-Spring UIs,
  494. you need to define the Spring servlet explicitly in the [filename]#web.xml#. You
  495. can map the servlet to any URL path, but perhaps typically, you define it as the
  496. default servlet as follows, and map the other servlets to other URL paths:
  497. [subs="normal"]
  498. ----
  499. &lt;web-app&gt;
  500. ...
  501. &lt;servlet&gt;
  502. &lt;servlet-name&gt;Default&lt;/servlet-name&gt;
  503. &lt;servlet-class&gt;
  504. com.vaadin.spring.internal.VaadinSpringServlet
  505. &lt;/servlet-class&gt;
  506. &lt;/servlet&gt;
  507. &lt;servlet-mapping&gt;
  508. &lt;servlet-name&gt;Default&lt;/servlet-name&gt;
  509. &lt;url-pattern&gt;[replaceable]##/myspringuis/*##&lt;/url-pattern&gt;
  510. &lt;/servlet-mapping&gt;
  511. &lt;servlet-mapping&gt;
  512. &lt;servlet-name&gt;Default&lt;/servlet-name&gt;
  513. &lt;url-pattern&gt;/VAADIN/+++*+++&lt;/url-pattern&gt;
  514. &lt;/servlet-mapping&gt;
  515. &lt;/web-app&gt;
  516. ----
  517. With such a setting, paths to Spring UIs would have base path [filename]#/myapp/myspringuis#, to which the (optional) UI path would be appended.
  518. The [filename]#/VAADIN/*# only needs to be mapped to the servlet if there are no other Vaadin servlets.
  519. (((range="endofrange", startref="term.advanced.spring.springlong")))
  520. (((range="endofrange", startref="term.advanced.spring.spring")))