新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么用SpringMVC创建Web应用

这篇文章给大家分享的是有关怎么用Spring MVC创建Web应用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

创新互联公司致力于互联网网站建设与网站营销,提供成都网站制作、成都网站建设、网站开发、seo优化、网站排名、互联网营销、成都小程序开发、公众号商城、等建站开发,创新互联公司网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

为了把Spring MVC运用到Web应用中,首先需要下载与操作系统对应的Spring软件包,下载地址为
https://repo.spring.io/libs-release-local/org/springframework/spring/
以下网址也提供了Spring软件包的下载:
www.javathinker.net/javaweb.jsp

1.1  建立Spring MVC的环境
把Spring软件包spring-framework-X.RELEASE-dist.zip解压到本地,把其中libs目录下的JAR文件考拷贝到Web应用的WEB-INF/lib目录下。图1-1展示了基于Sping MVC的helloapp应用的目录结构。

怎么用Spring MVC创建Web应用

图1-1  helloapp应用的目录结构

1.2  创建视图
Spring MVC的视图是一组包含了Spring标签的JSP文件。在本例中,视图层包括student.jsp和result.jsp两个文件。student.jsp负责生成一个HTML表单,让客户端输入学生信息。student.jsp的HTML表单由URL为“/helloapp/addStudent”的Web组件来处理:


 …

student.jsp使用了Spring 标签库中的标签。以下例程1-1是student.jsp的代码。
例程1-1  student.jsp

<%@page contentType = "text/html;charset = UTF-8" language = "java" %>
<%@taglib uri = "http://www.springframework.org/tags/form" 
                                              prefix = "form"%>

  
    Spring MVC Sample
  

  
    

Student Information

                                                                                                                                                                                               
Name
Age
ID
          

以上student.jsp代码中的标签来自于Spring标签库,用来生成HTML表单。
result.jsp负责显示客户端输入的学生信息,例程1-2是它的源代码。
例程1-2  result.jsp

<%@page contentType = "text/html;charset = UTF-8" language = "java" %>
<%@page isELIgnored = "false" %>
<%@taglib uri = "http://www.springframework.org/tags/form" 
                                                  prefix = "form"%>

  
   Spring MVC Sample
  

  
    

Submitted Student Information

                                                                                                         
Name:${name}
Age:${age}
ID:${id}
     

1.3  创建模型
在Spring MVC的模型层,可以创建表示业务数据或实现业务逻辑的JavaBean组件。以下例程1-3的Student类是一个JavaBean,它表示本范例应用的业务数据。
例程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;
  }
}

对于非常简单的JavaWeb应用,业务逻辑也可以直接由控制器层的Controller来完成。在本例中,业务逻辑将直接由StudentController来完成。

1.4  创建Controller组件
下面创建一个类名叫StudentController的Controller组件,参见例程1-4。StudentController类有两个方法:
   student()方法:对应的URL为“/student”,请求方式为HTTP GET方式。
   addStudent()方法:对应的URL为“/addStudent”,请求方式为HTTP POST方式。
例程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;

@Controller
public 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";
  }
}

当客户端以HTTP GET方式请求访问http://localhost:8080/helloapp/student,Spring MVC的DispatcherServlet就会把请求转发給StudentController的student()方法,这个方法返回一个ModelAndView对象,它表示把模型数据和视图绑定在一起的对象。在本例中,“new ModelAndView("student", "command", new Student())”中的三个参数的含义如下:
   第一个参数“student”表示视图组件的逻辑名字为“student”,实际上对应WEB-INF/jsp/student.jsp文件。本章1.5节会介绍如何在Spring MVC配置文件中配置这种对应关系。
   第二个参数“command”表明逻辑名为“student”的视图组件中的HTML表单需要与第三个参数指定的Student对象绑定。
   第三个参数“new Student()”提供了一个新建的Student对象。Spring MVC框架会负责把客户端在HTML表单中输入的数据填充到这个Student对象中。

DispatcherServlet接收到StudentController的student()方法返回的ModelAndView对象后,会把请求再转发給逻辑名字为“student”的视图组件,即WEB-INF/jsp/student.jsp文件。
以下图1-2显示了Spring MVC框架响应“/student”URL的流程。
怎么用Spring MVC创建Web应用

图1-2  Spring MVC框架响应“/student”URL的流程

student.jsp生成的网页如图1-3所示。
怎么用Spring MVC创建Web应用
图1-3  student.jsp生成的网页

客户在图1-3所示的HTML表单中输入学生的相关信息,然后提交表单,这时浏览器会以POST方式请求访问“/helloapp/addStudent”URL。
Spring MVC框架的DispatcherServlet接受到客户端的请求后,先把包含学生信息的HTML表单数据填充到表示模型数据的Student对象中,接下来DispatcherServlet就把请求转发給StudentController的addStudent()方法。
StudentController的addStudent()方法读取Student对象的各个属性,再把它存放到一个ModelMap对象中:

//model变量为ModelMap类型
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());

StudentController的addStudent()方法接下来返回一个字符串“result”,它是一个Web组件的逻辑名字,实际上对应WEB-INF/jsp/result.jsp文件。DispatcherServlet再把请求转发給result.jsp文件。result.jsp文件中的${name}、${age}和${id}标记会显示由StudentController存放在ModelMap对象中的name、age和id属性的值。由此可见,控制层可以借助ModelMap对象向视图层传递数据。
以下图1-4是result.jsp返回的包含学生信息的网页。
怎么用Spring MVC创建Web应用
图1-4  result.jsp返回的包含学生信息的网页

以下图1-5显示了Spring MVC框架响应“/helloapp/addStudent”URL的流程。
怎么用Spring MVC创建Web应用

图1-5  Spring MVC框架响应“/helloapp/addStudent”URL的流程

1.5  创建web.xml文件和Spring MVC 配置文件
在web.xml文件中,应该对Spring MVC框架的中央控制枢纽DispatcherServlet进行配置:





  Spring MVC Sample

  
    HelloWeb
    
      org.springframework.web.servlet.DispatcherServlet
    
    1
  

  
    HelloWeb
    /
  

以上代码为DispatcherServlet映射的URL为“/”,这意味着所有访问helloapp应用的客户请求都会先由DispatcherServlet来预处理,然后再由DispatcherServlet转发给后续组件。
以上代码为DispatcherServlet设置的Servlet名字为“HelloWeb”,与此对应,必须为Spring MVC框架提供一个名为HelloWeb-servlet.xml配置文件,它也存放在WEB-INF目录下。例程1-5是HelloWeb-servlet.xml文件的代码。
例程1-5  HelloWeb-servlet.xml



  

  

    
    
  

以上代码指定负责解析视图组件的逻辑名字的类为“InternalResourceViewResolver”。它的prefix和suffix属性分别设定了视图文件的前缀与后缀。
例如,对于StudentController的addStudent()方法返回的逻辑名字“result”,将被解析为“/WEB-INF/jsp/result.jsp”文件。
再例如,StudentController的student()方法返回一个ModelAndView对象,它包含的视图组件的逻辑名字为“student”,“student”将被解析为“/WEB-INF/jsp/student.jsp”文件。

1.6  运行helloapp应用
按以上步骤创建好helloapp应用后,就可以启动Tomcat服务器,运行helloapp应用。在源代码包的sourcecode/helloapp目录下,提供了这个应用的所有源文件,可以直接将整个helloapp目录拷贝到/webapps目录下,就会发布这个应用。
通过浏览器访问:
http://localhost:8080/helloapp/student
就可以访问helloapp应用了。

感谢各位的阅读!关于“怎么用Spring MVC创建Web应用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


文章名称:怎么用SpringMVC创建Web应用
文章路径:http://www.wjwzjz.com/article/jshcgp.html
在线咨询
服务热线
服务热线:028-86922220
TOP