用Eclipse建spring mvc项目(图解)


我参考了一个博客来建项目,网址是:http://limingnihao.iteye.com/blog/830409。按照这个博客的流程创建了项目,但是由于实际情况的不同,中间还是有一些不一样的。

下面我把我用到的相关软件版本列一下,虽然我不知道这些有什么关系,但是,还是列一下吧,害羞哈哈哈~~


因为不知道列哪些,所以就把这些都截了个图害羞,只知道要用jdk,maven,tomcat,和java编译器(如eclipse)

现在开始建项目

1、创建一个maven项目

 

图1


图2


图3(第一个复选框一定要框,反正我这里不选就是错~)



图4(Packaging选择war。反正我之前选jar就有问题了)




图5(红框中显示的不是我想要的,需要修改,右击它,选择“Build Path” ==>“ConfigureBuild Path”)


图6


图7(不知道为什么选这个,只是之前看同事选了这个。)


图8



图9


到这一步,已经算是建好了一个mave项目。我们现在来配置一些东西。

2、配置pom.xml(可以直接添加到pom.xml里面,配置一些七七八八的东西。junit、jstl、 spring包

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <dependencies>  
  2.         <!-- junit -->  
  3.         <dependency>  
  4.             <groupId>junit</groupId>  
  5.             <artifactId>junit</artifactId>  
  6.             <version>4.12-beta-3</version>  
  7.             <scope>test</scope>  
  8.         </dependency>  
  9.         <!-- log4j -->  
  10.         <dependency>  
  11.             <groupId>log4j</groupId>  
  12.             <artifactId>log4j</artifactId>  
  13.             <version>1.2.17</version>  
  14.         </dependency>  
  15.           
  16.         <dependency>  
  17.             <groupId>jstl</groupId>  
  18.             <artifactId>jstl</artifactId>  
  19.             <version>1.2</version>  
  20.         </dependency>  
  21.           
  22.         <!-- spring 的基本依赖 开始 -->  
  23.   
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-core</artifactId>  
  27.             <version>4.1.2.RELEASE</version>  
  28.         </dependency>  
  29.   
  30.         <dependency>  
  31.             <groupId>org.springframework</groupId>  
  32.             <artifactId>spring-expression</artifactId>  
  33.             <version>4.1.2.RELEASE</version>  
  34.         </dependency>  
  35.   
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-beans</artifactId>  
  39.             <version>4.1.2.RELEASE</version>  
  40.         </dependency>  
  41.   
  42.         <dependency>  
  43.             <groupId>org.springframework</groupId>  
  44.             <artifactId>spring-aop</artifactId>  
  45.             <version>4.1.2.RELEASE</version>  
  46.         </dependency>  
  47.   
  48.         <dependency>  
  49.             <groupId>org.springframework</groupId>  
  50.             <artifactId>spring-context</artifactId>  
  51.             <version>4.1.2.RELEASE</version>  
  52.         </dependency>  
  53.         <dependency>  
  54.             <groupId>org.springframework</groupId>  
  55.             <artifactId>spring-context-support</artifactId>  
  56.             <version>4.1.2.RELEASE</version>  
  57.         </dependency>  
  58.   
  59.         <dependency>  
  60.             <groupId>org.springframework</groupId>  
  61.             <artifactId>spring-tx</artifactId>  
  62.             <version>4.1.2.RELEASE</version>  
  63.         </dependency>  
  64.   
  65.         <dependency>  
  66.             <groupId>org.springframework</groupId>  
  67.             <artifactId>spring-web</artifactId>  
  68.             <version>4.1.2.RELEASE</version>  
  69.         </dependency>  
  70.         <dependency>  
  71.             <groupId>org.springframework</groupId>  
  72.             <artifactId>spring-jdbc</artifactId>  
  73.             <version>4.1.2.RELEASE</version>  
  74.         </dependency>  
  75.   
  76.         <dependency>  
  77.             <groupId>org.springframework</groupId>  
  78.             <artifactId>spring-webmvc</artifactId>  
  79.             <version>4.1.2.RELEASE</version>  
  80.         </dependency>  
  81.         <dependency>  
  82.             <groupId>org.springframework</groupId>  
  83.             <artifactId>spring-aspects</artifactId>  
  84.             <version>4.1.2.RELEASE</version>  
  85.         </dependency>  
  86.   
  87.         <dependency>  
  88.             <groupId>org.springframework</groupId>  
  89.             <artifactId>spring-test</artifactId>  
  90.             <version>4.1.2.RELEASE</version>  
  91.         </dependency>  
  92.         <!-- spring 的基本依赖 结束 -->  
  93.   </dependencies>  

3、在webapp下添加一个文件夹“WEB-INF”,在文件夹下添加一个文件“web.xml”(下面的内容可以全部复制过去)

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     version="2.5">  
  5.     <!-- 区分项目名称,防止默认重名 -->    
  6.     <context-param>    
  7.         <param-name>webAppRootKey</param-name>    
  8.         <param-value>maven.example.root</param-value>    
  9.     </context-param>    
  10.     
  11.     <!-- Spring的log4j监听器 -->    
  12.     <listener>    
  13.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    
  14.     </listener>    
  15.     
  16.     <!-- 字符集 过滤器  -->    
  17.     <filter>    
  18.         <filter-name>CharacterEncodingFilter</filter-name>    
  19.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  20.         <init-param>    
  21.             <param-name>encoding</param-name>    
  22.             <param-value>UTF-8</param-value>    
  23.         </init-param>    
  24.         <init-param>    
  25.             <param-name>forceEncoding</param-name>    
  26.             <param-value>true</param-value>    
  27.         </init-param>    
  28.     </filter>    
  29.     <filter-mapping>    
  30.         <filter-name>CharacterEncodingFilter</filter-name>    
  31.         <url-pattern>/*</url-pattern>    
  32.     </filter-mapping>    
  33.     
  34.     <!-- Spring view分发器 -->    
  35.     <servlet>    
  36.         <servlet-name>dispatcher0121</servlet-name>    
  37.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  38.         <init-param>    
  39.             <param-name>contextConfigLocation</param-name>    
  40.             <param-value>/WEB-INF/dispatcher0121-servlet.xml</param-value>    
  41.         </init-param>    
  42.         <load-on-startup>1</load-on-startup>    
  43.     </servlet>    
  44.     <servlet-mapping>    
  45.         <servlet-name>dispatcher0121</servlet-name>    
  46.         <url-pattern>*.do</url-pattern>    
  47.     </servlet-mapping>    
  48.   
  49. </web-app>  
web.xml文件里有几个值得注意的地方,上面标红的“dispatcher0121”可以修改为你想要的字符串,但是这个三个地方一定要一样。[servlet-name]+“-servlet.xml”这个是spring配置文件的文件名的命名规则。

4、在“webapp/WEB-INF/”下再添加一个文件“dispatcher0121-servlet.xml”,这个文件就是上面说的东西(可以全部复制到新建的文件中)

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"     
  3.        xmlns:aop="http://www.springframework.org/schema/aop"     
  4.        xmlns:context="http://www.springframework.org/schema/context"    
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"     
  6.        xmlns:tx="http://www.springframework.org/schema/tx"     
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  8.        xsi:schemaLocation="http://www.springframework.org/schema/aop     
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
  10.         http://www.springframework.org/schema/beans     
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  12.         http://www.springframework.org/schema/context     
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  14.         http://www.springframework.org/schema/mvc     
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd     
  16.         http://www.springframework.org/schema/tx     
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    
  18.     
  19.     <mvc:annotation-driven />    
  20.     <context:component-scan base-package="example0121" />    
  21.     
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  23.         <property name="prefix" value="/WEB-INF/views/" />    
  24.         <property name="suffix" value=".jsp" />    
  25.     </bean>    
  26.     
  27. </beans>    

dispatcher0121-servlet.xml文件中也有要注意的地方,"example0121"这个是接下来会用的包,名字可以随意,但是不能全部是数字。我不想说,我就是偷懒用数字,导致之前建的项目一直有问题。 害羞

图10

5、创建一个Controller层测试类(这个是什么东西,我也不知道。只知道怎么操作)



图11

按照这个操作,可以添加包也可以添加类,先添加类,要是发现在包那里没有写“example0121”,那就取消,然后添加一个包,包的名字是“example0121”。然后再创建一个叫“GeneralController”的类。

图12

然后将代码写好,像下面一样

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package example0121;  
  2.   
  3. import org.springframework.stereotype.Controller;    
  4. import org.springframework.ui.Model;    
  5. import org.springframework.web.bind.annotation.RequestMapping;    
  6.     
  7. @Controller    
  8. public class GeneralController {    
  9.     
  10.     @RequestMapping(value="index.do")    
  11.     public void index_jsp(Model model){    
  12.         model.addAttribute("str0121", "Hellow world");    
  13.         System.out.println("index.jsp");    
  14.     }    
  15. }    

6、编写jsp页面。在“webapp/WEB-INF/”下添加一个文件夹“views”,再添加一个文件“index.jsp”。这个路径是在“dispatcher0121-servlet.xml”里面定下来的。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>    
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
  4. <html>    
  5.     <head>    
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
  7.         <title>Insert title here</title>    
  8.     </head>    
  9.         
  10.     <body>    
  11.         <c:out value="${str0121}"></c:out>    
  12.     </body>    
  13. </html>    
这个时候你可以看到你的工程出现红色的叉叉,报了个错,如图13。这个时候你要淡定,两种方法。一是无视它,二是删掉这个错。我选择了无视它,我因为这个错查了好久资料,莫名烦躁,试了网上说的方法,还是不行。最后问了同事,同事说,你运行一下可不可以用。我运行了项目,发现可以用。然后同事说,那就无视它。似乎好像是校验的问题,反正现在不影响。one or two?!


图13

7、运行

图14


图15


图16


图17


图18

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值