从最基本的Netbeans默认Servlet项…

来源:互联网 发布:小额贷款骗局知乎 编辑:程序博客网 时间:2024/06/09 20:40
  首先我们在Netbeans下新建一个Servlet项目
  然后我们可以看到,它的默认目录结构是这样的。
从最基本的Netbeans默认Servlet项目开始配置到SpringMVC
  我们如何改成一个标准的SpringMVC框架的web项目呢?
【复习】我们都知道,servlet下会有个web.xml文件进行一系列路径映射配置。但是目前似乎啥都没有,于是自己建一个吧。SpringMVC不管怎么说,还是基于Servlet的,所以得配置一个接口,让SpringMVC彻底接管,然后我们才能在SpringMVC的框架下编程。
  首先让我们导入库文件。右键点击库,然后选择添加库,全局库里面的Spring Framework4.0.1就好。没错我们以这个版本进行练习。
  接着,在WEB-INF里面新建一个文件,内容如下:
文件web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       id="WebApp_ID"version="3.0">
   <!--
      <display-name>HelloSpringMVC2</display-name>
       这个display-name属性意义不大,就是个说明性的文字
   -->
  <servlet>
      <servlet-name>SpringMVC2servlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:beans.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
       <!--
          load-on-startup必须放在最后。这是启动顺序,让这个Servlet随Servletp
          容器一起启动,必须放在<servlet>配置的最后
       -->
  </servlet>
    <!-- Spring MVC配置文件结束-->
   
   <!--
      这里配置地址拦截请求。意思是把所有根目录/的请求,都配置给HelloSpringMVC2来运行
       所以,SpringMVC就把所有Servlet的请求接管过来了。
   -->
  <servlet-mapping>
      <servlet-name>SpringMVC2servlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
   
</web-app>
  我们从代码可以看出,原本一个地址配备一个class是servlet的工作模式。现在,我们把根地址及以上(也就是所有web请求)都交给了org.springframework.web.servlet.DispatcherServlet来处理。于是,我们整个servlet就连接上了SpringMVC框架。然后我们就可以在SpringMVC上编程了。
  注意看classpath部分,我们定义了一个xml文件叫做beans.xml,就像我们之前单独使用Spring框架一样。按照之前的讲解,beans(豆)里面肯定有很多bean(豆豆)吧,这些都是类工厂,供我们可以使用。这里其实大部分bean是供给SpringMVC使用的。我们在源包那新建一个beans.xml。至于为什么要在源包,而不在web页里面,因为param-value标签里用的是classpath,即类包。文件存在类包的目录里,
从最基本的Netbeans默认Servlet项目开始配置到SpringMVC
文件beans.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   
    <!--默认的注解映射的支持 -->  
  <mvc:annotation-driven/>
    
    <!--如果当前请求为“/”时,则转发到“/helloworld/index” -->
   <mvc:view-controllerpath="/"view-name="forward:/helloworld/index"/> 
    <!--静态资源映射 -->
   <mvc:resourcesmapping="/js
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/helloworld")
public class HiFirstApp{

   @RequestMapping(value="/index", method= {RequestMethod.GET})
   public ModelAndView index(){
      ModelAndView modelAndView = new ModelAndView(); 
      modelAndView.addObject("message","Hello World!HelloSpringMVC2"); 
      modelAndView.setViewName("index"); 
       returnmodelAndView;
   }
}
  在这里,通过setViewName指定了一个输出的视图,这个视图的名字叫index。那么是以什么为后缀的index呢?还记得在beans.xml末尾有这样一个标签吗?
<propertyname="suffix"value=".jsp"/>
  也就是意味着,这里代表的是index.jsp,以jsp结尾的文件作为视图文件。
  同时我们也看到,通过addObject方法,对message字段添加了“Hello World!HelloSpringMVC2”,这就是往视图里添加处理后的信息了。我们看看视图文件是怎么定义的:
文件index.jsp
<%@ pagelanguage="java"contentType="text/html;charset=UTF-8"
       pageEncoding="UTF-8"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
      <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
      <title>Insert titlehere</title>
  </head>
  <body>
      ${message}
  </body>
</html>
  看到红字部分的message了吗?程序中的addObject方法就是把这个message字段更改为了HelloWorld!HelloSpringMVC2。至此,我们整个SpringMVC框架搭建完毕,一个app也写好了。让我们点击运行,部署在tomcat上看看效果。有人问剩下一个在项目里的文件context.xml怎么办?倒是可以不用更改。不过你觉得web地址栏太长的话,这里给出效果和对应关系:
从最基本的Netbeans默认Servlet项目开始配置到SpringMVC






0 0
原创粉丝点击