Simple examples of using annotations for spring mvc

Keywords: JSP Spring xml Attribute

Examples of using annotations
The first step is to create a project to import related jar packages
Create a web.xml file
Here's the main code


Write back-end controller

public class SimpleAnnotationControl {
public ModelAndView show() {
ModelAndView mav = new ModelAndView("anno");
mav.addObject("message", "Successful use of annotations");
return mav;

@ The purpose of the Controller annotation is to add back-end controllers to the spring container
@ RequestMapping uses the abbreviation pattern here: in fact, its prototype is @RequestMapping(value ="/"). The value attribute indicates its mapping path, such as the mapping path here.
For This note removes the value attribute and has the following attributes: method, headers, params.

Then create the spmvc-servlet.xml file

<?xmlversion="1.0" encoding="UTF-8"?>
<beans xmlns=""
<context:component-scan base-package="com.asm" />
<bean id="irViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />

Then write / WEB-INF/anno.jsp file


Then visit the ____________. / Successful debugging of
By default, get access can also be used to access the well-known post @RequestMapping(value ="/", method = RequestMethod.Post) relative to get method.
Next we configure a request instance for the class
First write back-end controller

public class AnnotationControl {
@RequestMapping(value = "/the/{name}.do")
public String getName(@PathVariable
String name, Model model) {
model.addAttribute("message", "Name:" + name);
return "anno";
public ModelAndView getAge(@RequestParam
int age) {
ModelAndView mav = new ModelAndView("anno");
mav.addObject("message", "Age:" + age);
return mav;

This controller indicates that methods in order to access this class must have... / MyRoot
For example, to access the getAge method, the access path must be... / MyRoot/
Let's first visit age.
Add the following code to the index.jsp page

<form action="<%=request.getContextPath() %>/myRoot/" method="post">
<input type="text" name="age">
<input type="submit" value="Age of submission">

In this way, we can get the age, @RequestParam annotation, which will bind the corresponding parameters in the method (corresponding to the name, for example, the form and method parameter names are used here).
web request parameters with age
Next, we use spring mvc annotation to complete the binding of form parameters and business layer entities.
First create

public class User {
private String username;
private String password;
//getter/setter method.

Then write the control layer code

public class FormAnnotationControl {
public User initUser() {
User user = new User();
user.setUsername("User name");
return user;
public String addUI() {
return "reg";
public String add(@ModelAttribute
User user, Model model) {
return "userInfo";
public ModelAndView login(@ModelAttribute
User user) {
ModelAndView mav = new ModelAndView(new RedirectView(""));
if (!"User name".equals(user.getUsername())) {
mav = new ModelAndView("error");
return mav;

:@ModelAttribute This annotation can be used for binding web request parameters and method parameters, or for returning values of methods as model data (which is globally valid in classes, such as initUser here).
Method-bound model data can be accessed in any method of this kind.
Then write relevant Jsp pages

<form action="<%=request.getContextPath() %>/" method="post">
User name: <input type="text" name="username" value="${user.username}"> <br/>
Cryptography: <input type="password" name="password" value="${user.password}"><br/>
<input type= "submit" value= "save user">


Save the user successfully with the following information: <br/>
Username: ${user.username} <br/>
Cryptocode: ${user.password}

Then open the server for debugging.

Posted by windwaker on Thu, 05 Sep 2019 00:47:46 -0700