Beruflich Dokumente
Kultur Dokumente
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Click 3:
Step toAdd
edit the
Master title style file
configuration
We need
Click to editto create
Master textastyles
configuration file name [servlet-name]-
servlet.xml,
Second levelwhere [servlet-name] is the name we gave our
servlet in Step
Third level2 (helloworld)
This is a normal
FourthSpring
level configuration file that defines a web
context
Fifth level
We tell it to look for classes in the com.hvn.helloworld
namespace, annotated with @Controller
<context:component-scan base-package="com.hvn.helloworld use-default-
filters="false">
<context:include-filter
expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
Click 3:
Step toContinue
edit Master title style
We also
Click need
to edit Masterto text
tell Spring
styles MVC that we are going to
configure it via annotations:
Second level
Third level
<mvc:annotation-driven />
Fourth level
If you map the DispatcherServlet
Fifth level to the default servlet (we
did), you need to add the following:
<mvc:default-servlet-handler />
Finally, we need to configure a ViewResolver to find our JSPs
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="redirectHttp10Compatible" value="false"/>
</bean>
Click to edit
Hierarchy of Master title style
Contexts
When
Click to we
edit create thestyles
Master text [servlet-name]-servlet.xml file, we are
creating
Secondalevel
new Spring context that is a child of your
application context
Third level
It can resolve beans
Fourth level from the root context, but other contexts
cant resolve beans from it
Fifth level
You can create as many of these as you need (ie, you might
have another one for web services)
You need to make sure you dont redefine beans, though
Click to edit
Hierarchy of Master title style
Contexts
Click to edit Master text styles
Second level a DefaultWebContext
/
Third level
Fourth level
Fifth level
b GWT Context
/*.gwtrpc
c Web Services
/ws/
Click torequest
Simple edit Master title style
mapping
We can
Click doMaster
to edit simple mappings
text styles to static content in the xml
configuration,
Second level which maps /helloworld to /WEB-
INF/views/hello.jsp
Third level
<bean id="helloWorldController"
Fourth level
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName"
Fifth level value="hello" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="1" />
<property name="urlMap">
<map>
<entry key="/hello" value-ref="helloWorldController" />
</map>
</property>
</bean>
Click1:toConfigure
Lab edit Master title style
a project
Taketoaedit
Click basic project
Master (with no view) created via Eclipse IDE (or
text styles
order)
Second level
Add dependencies,
Third level web.xml config, and servlet config
Add a simple request
Fourth level mapping
Verify that it works
Fifth level
Click toController
Simple edit Master title style
For most
Click to editcases,
Masteryoull need to create a controller
text styles
Create
Seconda class
level and annotate it with @Controller
Then, create a method annotated with a @RequestMapping
Third level
Fourth level
package com.hvn.helloworld;
Fifth level
@Controller
public class HelloWorldController {
@RequestMapping(value="/")
public String hello() {
return hello;
}
}
Click to editRequest
Advanced Master title style
Mapping
RequestMappings
Click to edit Master textare really flexible
styles
You can define
Second level a @RequestMapping on a class and all
method @RequestMappings
Third level will be relative to it.
There are a number
Fourth level of ways to define them:
URI Patterns Fifth level
HTTP Methods (GET, POST, etc)
Request Parameters
Header values
Click to edit Master title
@RequestMapping style
Class level
Click to edit Master text styles
package com.hvn.helloworld;
Second level
Third level
@RequestMapping("/customer")
@Controller Fourth level
public class CustomerController
Fifth level {
@RequestMapping("/create")
public String create() {
return create";
}
}
Click to edit Master title
@RequestMapping style
HTTP
Methods
Click to edit Master text styles
package com.hvn.hellworld;
Second level
Third level
@RequestMapping("/customer")
@Controller Fourth level
public class CustomerController
Fifth level {
The url for this (relative to your context root) would be:
/customer/1/viewProject/10
Click2:toCreate
Lab edit Master title style
a controller
Create
Click a simple
to edit Master controller
text styles for /hello
Return
Seconda jsp
level
Experiment with some more advanced request mappings
Third level
Fourth level
Fifth level
Click to edit
Controller MasterArguments
Method title style
Sometimes
Click you need
to edit Master access to the request, session, request
text styles
body, or other
Second level items
If you add
Thirdthem
level as arguments to your controller method,
Spring will pass
Fourththem
level in
@RequestMapping(value="/")
Fifth level
public String getProject(HttpServletRequest request,
HttpSession session,
@RequestParam(projectId) Long projectId,
@RequestHeader("content-type") String contentType) {
return "index";
}
Click to editAnnotations
Supported Master title on
style
params
@PathVariable
Click to edit Master text styles
@RequestParam
Second level
@RequestHeader
Third level
@RequestBody
Fourth level
Fifth level
Click to edit
Method Master title
Arguments style
(Samples)
This gives
Click to edityou access
Master to the request/response and session
text styles
Second level
@RequestMapping(value="/")
ThirdgetProject(HttpServletRequest
public String level request,
HttpServletResponse
Fourth level response,
HttpSession session) {
Fifth level
return "index";
}
Click to edit
Method Master title
Arguments style
(Samples)
This gives
Click to edityou access
Master to request parameters and headers
text styles
Second level
@RequestMapping(value="/")
Third level
public String getProject(
Fourth level
@RequestParam Long projectId,
Fifth level
@RequestHeader("content-type") String contentType) {
return "index";
}
Click to edit
Method Master title
Arguments style
(Samples)
@PathVariable
Click to edit Masterties
textdynamic
styles elements of the URI to method
arguments
Second level
Third level
@RequestMapping(value="/project/{portfolioId}/{projectId}")
public String getProject(
Fourth level
@PathVariable(projectId) Long id,
Fifth @PathVariable
level Long portfolioId) {
return "index";
}
ClickModel
The to edit Master title style
You to
Click populate thetext
edit Master view with data by via the ModelMap or
styles
ModelAndView
Second level (which has a ModelMap underneath)
This is basically
Third levela Map
All attributes are level
Fourth added to the request so that they can be
picked up by JSPs
Fifth level
Click to edit Master title style
ModelMap
Add it edit
Click to as aMaster
parameter to your controller method
text styles
public Second
String leveldoController(ModelMap modelMap){
modelMap.addAttribute(user);
Third level
modelMap.addAttribute(otherUser,
Fourth level user);
return index;
} Fifth level