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
<servlet-name>spmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
Write back-end controller
@Controller public class SimpleAnnotationControl { @RequestMapping("/anno.do") 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 ="/anno.do"). The value attribute indicates its mapping path, such as the mapping path here.
For anno.do. 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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <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" /> </bean> </beans>
Then write / WEB-INF/anno.jsp file
<body> ${message} </body>
Then visit the ____________. / Successful debugging of anno.do
By default, get access can also be used to access the well-known post @RequestMapping(value ="/anno.do", method = RequestMethod.Post) relative to get method.
Next we configure a request instance for the class
First write back-end controller
@Controller @RequestMapping("/myRoot") public class AnnotationControl { @RequestMapping(value = "/the/{name}.do") public String getName(@PathVariable String name, Model model) { model.addAttribute("message", "Name:" + name); return "anno"; } @RequestMapping("/age.do") 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/age.do
Let's first visit age.
Add the following code to the index.jsp page
<form action="<%=request.getContextPath() %>/myRoot/age.do" method="post"> <input type="text" name="age"> <input type="submit" value="Age of submission"> </form>
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 User.java
public class User { private String username; private String password; //getter/setter method. }
Then write the control layer code
@Controller public class FormAnnotationControl { @ModelAttribute("user") public User initUser() { User user = new User(); user.setUsername("User name"); return user; } @RequestMapping("reg.do") public String addUI() { return "reg"; } @RequestMapping("save.do") public String add(@ModelAttribute User user, Model model) { model.addAttribute(user); return "userInfo"; } @RequestMapping("login.do") public ModelAndView login(@ModelAttribute User user) { ModelAndView mav = new ModelAndView(new RedirectView("manage.do")); 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
reg.jsp
<form action="<%=request.getContextPath() %>/save.do" 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"> </form>
userInfo.jsp
<body> Save the user successfully with the following information: <br/> Username: ${user.username} <br/> Cryptocode: ${user.password} </body>
Then open the server for debugging.