In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Spring MVC to create Web applications. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In order to apply Spring MVC to Web applications, you first need to download the Spring package corresponding to the operating system at
Https://repo.spring.io/libs-release-local/org/springframework/spring/
Downloads of Spring packages are also available at the following URL:
Www.javathinker.net/javaweb.jsp
1.1 establish an environment for Spring MVC
Extract the Spring package spring-framework-X.RELEASE-dist.zip to the local location, and copy the JAR file in the libs directory to the WEB-INF/lib directory of the Web application. Figure 1-1 shows the directory structure of Sping MVC-based helloapp applications.
Figure 1-1 directory structure of helloapp applications
1.2 create a view
The view of Spring MVC is a set of JSP files that contain Spring tags. In this example, the view layer includes two files, student.jsp and result.jsp. Student.jsp is responsible for generating an HTML form that allows the client to enter student information. The HTML form for student.jsp is processed by the Web component with a URL of "/ helloapp/addStudent":
...
Student.jsp uses tags from the Spring tag library. The following routine 1-1 is the code for student.jsp.
Routines 1-1 student.jsp
Spring MVC Sample Student Information Name Age ID
The, and tags in the above student.jsp code come from the Spring tag library, which is used to generate HTML forms.
Result.jsp is responsible for displaying the student information entered by the client, and routines 1-2 are its source code.
Routines 1-2 result.jsp
Spring MVC Sample Submitted Student Information Name: ${name} Age: ${age} ID: ${id}
1.3 create a model
At the model layer of Spring MVC, you can create JavaBean components that represent business data or implement business logic. The Student class of the following routines 1-3 is a JavaBean that represents the business data of this example application.
Routines 1-3 Student.java
Package mypack;public class Student {private Integer age; private String name; private Integer id; public void setAge (Integer age) {this.age = age;} public Integer getAge () {return age;} public void setName (String name) {this.name = name;} public String getName () {return name;} public void setId (Integer id) {this.id = id;} public Integer getId () {return id;}}
For very simple JavaWeb applications, the business logic can also be directly completed by the Controller of the controller layer. In this case, the business logic will be done directly by StudentController.
1.4 create Controller components
Let's create a Controller component with a class called StudentController, as shown in routines 1-4. The StudentController class has two methods:
student () method: the corresponding URL is "/ student", and the request method is HTTP GET.
addStudent () method: the corresponding URL is "/ addStudent", and the request method is HTTP POST.
Routines 1-4 StudentController.java
Package mypack;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import org.springframework.ui.ModelMap;@Controllerpublic class StudentController {@ RequestMapping (value = "/ student", method = RequestMethod.GET) public ModelAndView student () {return new ModelAndView ("student", "command", new Student ()) @ RequestMapping (value = "/ addStudent", method = RequestMethod.POST) public String addStudent (@ ModelAttribute ("SpringWeb") Student student,ModelMap model) {model.addAttribute ("name", student.getName ()); model.addAttribute ("age", student.getAge ()); model.addAttribute ("id", student.getId ()); return "result";}}
When the client requests access to http://localhost:8080/helloapp/student MVC Spring MVC's DispatcherServlet in HTTP GET mode, it forwards the request to StudentController's student () method, which returns a ModelAndView object that represents the object that binds the model data to the view. In this example, the meaning of the three parameters in "new ModelAndView (" student "," command ", new Student ()) is as follows:
The first parameter "student" of indicates that the logical name of the view component is "student", which actually corresponds to the WEB-INF/jsp/student.jsp file. Section 1.5 of this chapter describes how to configure this correspondence in the Spring MVC configuration file.
The second parameter "command" of indicates that the HTML form in the view component logically named "student" needs to be bound to the Student object specified by the third parameter.
The third parameter "new Student ()" of provides a newly created Student object. The Spring MVC framework is responsible for populating the data entered by the client in the HTML form into the Student object.
After receiving the ModelAndView object returned by the student () method of StudentController, DispatcherServlet forwards the request to the view component with the logical name "student", that is, the WEB-INF/jsp/student.jsp file.
Figures 1-2 below show the flow of the Spring MVC framework responding to the "/ student" URL.
Figure 1-2 flow of the Spring MVC framework responding to "/ student" URL
The web page generated by student.jsp is shown in figure 1-3.
Figure 1-3 Web pages generated by student.jsp
The customer enters the relevant information about the student in the HTML form shown in figure 1-3, then submits the form, and the browser requests access to the "/ helloapp/addStudent" URL in POST.
After receiving the request from the client, the DispatcherServlet of the Spring MVC framework populates the HTML form data containing the student information into the Student object representing the model data, and then DispatcherServlet forwards the request to the addStudent () method of StudentController.
The addStudent () method of StudentController reads the properties of the Student object and stores it in a ModelMap object:
/ / model variable is ModelMap type model.addAttribute ("name", student.getName ()); model.addAttribute ("age", student.getAge ()); model.addAttribute ("id", student.getId ())
The addStudent () method of StudentController then returns a string "result", which is the logical name of a Web component and actually corresponds to the WEB-INF/jsp/result.jsp file. DispatcherServlet then forwards the request to the result.jsp file. The ${name}, ${age}, and ${id} tags in the result.jsp file display the values of the name, age, and id properties stored in the ModelMap object by StudentController. Thus, the control layer can transfer data to the view layer with the help of ModelMap objects.
Figure 1-4 below is a web page returned by result.jsp that contains student information.
Figure 1-4 Web page containing student information returned by result.jsp
Figures 1-5 below show the flow of the Spring MVC framework responding to the "/ helloapp/addStudent" URL.
Figure 1-5 flow of the Spring MVC framework responding to "/ helloapp/addStudent" URL
1.5 create web.xml files and Spring MVC configuration files
In the web.xml file, DispatcherServlet, the central control hub of the Spring MVC framework, should be configured:
Spring MVC Sample HelloWeb org.springframework.web.servlet.DispatcherServlet 1 HelloWeb /
The URL of the above code for DispatcherServlet mapping is "/", which means that all customer requests to access helloapp applications are preprocessed by DispatcherServlet and then forwarded by DispatcherServlet to subsequent components.
The above code sets the Servlet name for DispatcherServlet as "HelloWeb". Accordingly, a configuration file named HelloWeb-servlet.xml must be provided for the Spring MVC framework, which is also stored in the WEB-INF directory. Routines 1-5 are the code for the HelloWeb-servlet.xml file.
Routines 1-5 HelloWeb-servlet.xml
The above code specifies that the class responsible for resolving the logical name of the view component is "InternalResourceViewResolver". Its prefix and suffix attributes set the prefix and suffix of the view file, respectively.
For example, the logical name "result" returned by the addStudent () method of StudentController will be resolved to a "/ WEB-INF/jsp/result.jsp" file.
For example, the student () method of StudentController returns a ModelAndView object that contains a view component with a logical name of "student" and "student" will be parsed to a "/ WEB-INF/jsp/student.jsp" file.
1.6 run the helloapp application
After you have created the helloapp application by following the steps above, you can start the Tomcat server and run the helloapp application. All the source files for the application are provided in the sourcecode/helloapp directory of the source code package, and the entire helloapp directory can be copied directly to the / webapps directory, and the application will be released.
Access through the browser:
Http://localhost:8080/helloapp/student
You can access the helloapp application.
Thank you for reading! This is the end of the article on "how to create Web applications with Spring MVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.