手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring学习之第一个Spring MVC程序(IDEA开发环境)

[日期:2016-06-25] 来源:Linux社区  作者:luoxn28 [字体: ]

  回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的时代。一般接触到一个新的Web框架,都会想问这个框架优势在哪?或者比其他框架好在哪里?如果没有使用Spring MVC框架,而是使用其他框架并且能够很好地满足要求,这样转换框架或许不是一个好主意。如果像我这样首次接触Web开发框架,Spring MVC还是值得优先考虑的。

  • Web层,更确切说是在框架处理器方面,Spring MVC对请求处理期间涉及的各种关注点进行了合理而完全的分离,并明确设置了响应的角色用于建模并处理整个声明周期中的各个关注点。其中包括:HandlerMapping用于处理Web请求与具体请求处理控制器的映射关系;LocaleResolver用于国际化处理;ViewResolver用于灵活的视图选择。
  • 从表现层来看,Spring MVC用了逻辑命名视图策略,通过引入ViewResolver和View,清晰分离了视图策略的选择和渲染与具体控制器之间的耦合,适合各种视图技术很容易集成到Spring MVC中,不管是JSP/JSTL作为视图技术,还是Velocity/FreeMarker,甚至是PDF/Excel等二进制格式视图形式,使用它们,只需要简单的配置。
  • Spring MVC还有另一个特色,就是“师出名门”,作为Spring大家族中的一员,很容易得到家族中其他兄弟的支持,从IoC到AOP的支持,以及数据访问层、事务管理层的支持等。Spring MVC属于请求驱动的Web框架,将单一Servlet作为整个应用的Front Controller,该Servlet收到具体的Web请求后,会参考设置的映射关系,将待处理的Web请求转发给次一级的控制器来处理。

建立Spring MVC工程

  LZ的开发环境是IDEA,这几天使用IDEA的感觉来看,IDEA整体界面比eclipse清爽,打开和关闭速度要快好多,但是好多功能还不知道在哪里,呜呜呜…

1 打开IDEA,新建工程

2 工程选择如下所示

  当然,新建工程时也可以选择Spring – Spring MVC(这里把Web Application也要选择上)

3 工程命名为mvcdemo,如下所示,然后点击finash

4 新建工程完成后,需要手动添加关联的jar包(LZ没有使用maven,为什么呢,因为现在还不会…),鼠标移动工程名处右键点击Open Module Settings,添加对应的Jar包

5 最后工程整体如下所示,为了方便,把spring所有的jar包都给添加上了,还有commons-logging包。

6 配置web.xml文件和mvcdemo-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Spring MVC</display-name>

    <servlet>
        <servlet-name>mvcdemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvcdemo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

  以上是web.xml文件,DispatcherServlet作为整个应用的Front Controller,其用来处理所有请求,而不能像“一个Web请求对应一个Servlet”那样获取Web容器对URL映射匹配的支持,而只好自己处理具体的Web请求和具体的处理类之间的映射关系了,也就是需要借助于ControllerBeanNameHandlerMapping了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- HandlerMapping 根据benaname找到对应的controller -->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>

    <!-- controller 配置处理器 -->
    <bean name="/hello" class="com.luoxn28.hello.HelloController"/>

    <!-- ViewResolver 视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 和 后缀 -->
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

  ControllerBeanNameHandlerMapping用来处理具体的Web请求和具体的处理类之间的映射关系,而Controller(HelloController是Controller的实现类)也就是一个具体的处理类。InternalResourceViewResolver配置的是视图解析相关信息。

7 在src文件夹下新建com.luoxn28.hello包,然后在包下新建HelloController类。

package com.luoxn28.hello;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController extends AbstractController {
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
        String hello = request.getParameter("hello");

        System.out.println("HelloController: " + hello);
        ModelAndView mav = new ModelAndView("hello");
        mav.addObject("hello", hello);

        return mav;
    }
}

8 在WEB-INF文件夹下新建hello.jsp文件,内容如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>springmvc</title>
</head>
<body>
    Spring MVC<br/>
    ${hello}
</body>
</html>

9 更改index.jsp文件如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <form action="hello" method="post">
      hello:<input type="text" name="hello"/>
      <input type="submit" value="提交"/>
    </form>
  </body>
</html>

10 至此,整个工程建立完毕,现在的工程视图如下:

 

  然后鼠标移动工程名处右键点击Open Module Settings,进行配置,在Modules下添加Spring,选择Spring Application Context为WEB-INF下的mvc-demo-servlet.xml,如下图所示:

  点击Artifacts,下面有提示信息,点击Fix...按钮,如下所示:

  一切设置完毕后,点击启动按钮,显示画面如下所示:

  随便输入一串字符串后点击提交,比如输入"luoxn28",最后会跳转到如下界面,到此整个工程结束。

 

参考资料

  1、《Spring揭秘》Spring MVC章节 http://www.linuxidc.com/Linux/2016-06/132070.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-06/132658.htm

linux
相关资讯       Spring MVC  Spring学习 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款