手把手实战:eclipse 搭建 SpringMvc 框架环境

环境准备

eclipse

jdk1.8

tomcat 7

 

步骤总纲

 

    a.准备好开发环境

    b.下载相关jar包

    c.在eclipse 中创建好工程

    d.引进下载的jar 包

    e.配置xml文件

    f.编写代码及测试

 

Springmvc相关jar包

jsp,servelet相关jar包

创建工程及导入jar包

在项目浏览器中右键 - >新建 - >动态Web项目 

 

有时候可能你的工作空间中并没有创建过类似的工程,所以在新的子菜单中并没有Dynamic Web Project,

那么你可以点其他,然后在弹出的对话框中找到Web,打开其折叠的项目可以找到Dynamic Web Project。

 

选择Dynamic Web Project后会有对话框弹出,对话框主要是设置一下工程的基础东西,如工程名,web服务器tomcat 

之类的,本例子采用eclipse集成的tomcat 7(如何把tomcat 7集成到eclipse中网上有很多教程,也很简单,相信你搜一下就可以知道了)。

填好之后可以直接finish,我得到的目录是如下所示。

 

 

然后,将下载到的所需要的jar包全部放到WebContent / WEB-INF / lib中去,

然后选中工程 - >右键 - >刷新,刷新一下,就可以看到jar包自动配到环境路径中了。

 

 

接下来是xml的配置文件,本例只需要配置两个xml文件。一个是web.xml,这是所有web项目都需要配置的;另一个是spring相关所需要配置文件,我这里将其命名为spring-mvc.xml,当然你也可以叫其他名字,只要在相应配置的地方别把你起的名字搞错就行了。

 

web.xml这个文件是放到WebContent / WEB-INF /路径下的。

web.xml中的内容如下

 

<?xml version =“1.0”encoding =“UTF-8”?>

<web-app version = “3.0”

     xmlns = “ http://java.sun.com/xml/ns/javaee ”

     xmlns:xsi = “ http://www.w3.org/2001/XMLSchema-instance ”

     xsi:schemaLocation = “ http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd “ >

<! - 配置springmvc的核心分发器 - > 

的<servlet>

    <servlet-name> springMVC </ servlet-name>

    <servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class>

    <! - 有哪些文件是要跟着一起初始的 - >

    <INIT-PARAM>

        <param-name> contextConfigLocation </ param-name>

        <param-value> classpath:spring - *。xml </ param-value>

    </ INIT-param>

    <load-on-startup>1</load-on-startup>

</servlet>

<!--核心分发器  要拦截哪些url --> 

<servlet-mapping>

    <servlet-name>springMVC</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>

     

</web-app>

首先注意到 version="3.0" , 

这是我创建工程 的时候有相关选项,这里得跟那个选择对应。

<!--有哪些文件是要跟着一起初始化的 -->

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring-*.xml </param-value>

    </init-param>

这个的意思就是有哪些配置文件要在启动时跟着初始化的,当然我们的另一个xml文件也是要初始化的。但是web容器的入口就是web.xml,其他的xml你不跟它说,它是不知道的。所以在此告知我们src下的长得像spring - *.xml 这样的文件也要初始化。前面我也说过,spring-mvc.xml这个文件名是我自行起的,你可以起别的名字,在web.xml中能清楚告诉人家你的叫什么就行了。

 

spring-mvc .xml是放到src下的

其内容为

<?xml version =“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 

    xmlns:jdbc =“ http://www.springframework.org/schema/jdbc ”  

    xmlns:jee =“ http://www.springframework.org/schema/jee 

    xmlns:tx =“ http://www.springframework.org/schema/tx 

    xmlns:aop =“ http://www.springframework.org/schema/aop 

    xmlns:mvc =“ http://www.springframework.org/schema/mvc 

    xmlns:util =“ http://www.springframework.org/schema/util 

    xmlns:jpa =“ http://www.springframework.org/schema/data/jpa 

    的xsi:的schemaLocation =”

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.3.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.3.xsd

http://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.3.xsd">

 

   <!-- 开启注解 -->

   <mvc:annotation-driven/>

   

    <!-- 扫描哪些包-->

   <context:component-scan base-package="com.hlm"/>

    

    <!-- 配置视图解析器 -->

     <!--prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),

        比如传进来的逻辑视图名为WEB-INF/jsp/index,则该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”; -->

     <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <property name="suffix" value=".jsp"/>

   </bean>

</豆>

注意,我用到的spring版本为4.3所以在该文件开头对应地方也是对应着我4.3的版本。如果你的spring是其他版本,请对应到你的版本上去。这些都是网上对应的文本,你不确定你写的是不是真有其文件的时候可以复制网址打开浏览器查询一下就知道了

创建所需要的包及文件。我创建了两个包:com.hlm.command,com.hlm.controller及一个文件夹jsp

分别创建两个类

com.hlm.command ;

/ **

 * <p>

 * <ul> 用户:对应hlm_users表的实体</ ul>

 * <li> userName:用户姓名</ li>

 * <li> 性别:性别0表示女,1表示男</ li>

 * <li> 令牌:注册令牌,手机或邮箱</ li>

 * <li> tokenType:令牌类型.0为手机,1为邮箱</ li>

 * <li> 密码:密码</ li>

 * </ p>

 * @author hlm

 *

 * /

公共  UsersCommand {

     private 字符串 用户名 ;

     私人 整数 性 ;

     私人 字符串 令牌 ;

     私人 整数 tokenType ;

     私人 字符串 密码 ;

     

     public UsersCommand(){

           

     }

     public UsersCommand(String userName ,Integer sex ,String token ,

                           整数 tokenType ,字符串 密码){

           这个。userName = userName ;

           这个。性别 = 性别;

           这个。标记 = 标记;

           这个。tokenType = tokenType ;

           这个。密码 = 密码;

     }

     public String getUserName(){

           返回 用户名;

     }

     public void setUserName(String userName ){

           这个。userName = userName ;

     }

     public Integer getSex(){

           返回 性别;

     }

     public void setSex(Integer sex ){

           这个。性别 = 性别;

     }

     public String getToken(){

           返回 令牌;

     }

     public void setToken(String token ){

           这个。标记 = 标记;

     }

     public Integer getTokenType(){

           返回 tokenType ;

     }

     public void setTokenType(Integer tokenType ){

           这个。tokenType = tokenType ;

     }

     public String getPassword(){

           返回 密码;

     }

     public void setPassword(String password ){

           这个。密码 = 密码;

     }          

}

 com.hlm.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.hlm.command.UsersCommand ;

@Controller

公共  UsersController {

           

     @RequestMapping (“/sigup.do” )                                                               

     public ModelAndView sigup(HttpServletRequest req ){

            ModelAndView mv = new ModelAndView();

           

           UsersCommand cmd = new UsersCommand(“小明” ,0,“ [email protected] ” ,1,“123456” );

        

            req .getSession()。setAttribute(“user” , cmd );

            mv .addObject(“password” , cmd .getPassword());

            mv .addObject(“userName” , cmd .getUserName());

            系统。out .println(cmd .getPassword());

            mv .setViewName(“index” );

        返回 mv ;

     }

}

两个页面相关的文件如下

 

 

 

<!DOCTYPE html>

< html >

< head >

< meta charset = “UTF-8” >

< title > 登录页面</ title >

</ head >

< body >

< 一个 HREF = “sigup.do” > 点我登录</ 一>

</ body >

</ html >

<%@ page language = “java” contentType = “text / html; charset = utf-8”

    pageEncoding = “utf-8”%>

<!DOCTYPE html PUBLIC “ - // W3C // DTD HTML 4.01 Transitional // EN” “ http://www.w3.org/TR/html4/loose.dtd ” >

< html >

< head >

< meta http-equiv = “Content-Type” content = “text / html; charset = utf-8” >

< title > 登录后的信息</ title >

</ head >

< body >

  < p > $ {password} </ p >

  < p > $ {userName} </ p >

</ bod>

 

 

 

所有的配置及代码都写好之后,准备部署到tomcat让它跑起来了。

找到如下位置,点击蓝色英文字来创建服务器 

如果在整个eclipse版面中你没有找到上面的Servers那么你可以到Window - > Show View - > Servers中将其找出来。

点击创建服务后,会出现如下弹窗。选择你在eclipse中配的tomcat就好了,我这里配的是tomcat 7。

创建之后还需要设置一下。

然后将我们的工程添加到这个服务上去。

 

启动成功之后,打开浏览器。我用的是默认端口:8080,如果你改变自己tomcat的端口,对应输入自己的就行了,然后后面是工程的名字。

回车之后就是默认的首页WebContent / index.html。点击“点我登录”,应该跳转到WebContent / WEB-INF / index.jsp。

 

 

有时候,如果你视图解析的地方如果配得不正确,它有可能找不到你的jsp的地址

 

 

 

git代码地址:https://github.com/mottohlm/springmvctest