idea创建springmvc项目

创建maven项目,利用webapp骨架生成项目架构

 

在main创建java项目作为源程序根目录

在pom.xml 导入springmvc所需要的依赖

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

在web.xml文件中,利用快捷键alt+insert可以快速创建springmvc中的前端转发设置

 

由于springmvc前置转发需要在项目部署的时候先创建对象,因此需要指定spring配置文件,contextConfigLocation 属性设置的就是指定的spring配置文件

load-on-startup属性设置该对象是否在部署前先创建,数值越低优先级越高

在resources中创建springbean.xml文件,作为springmvc项目的配置文件。

在配置文件中设置需要扫描的文件夹。

同时设置视图解析器,如果有视图解析器如果方法需要返回到其他页面时,仅需返回页面的名字,如return "hello"否则需要return "位置/hello.jsp"

    <context:component-scan base-package="com.lin"></context:component-scan>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

配置tomcat服务器

 !出现过一个问题,如果web.xml中dispatcher的url-pattern是/*,会发生404错误

 如果改成“/”则可以成功运行

 

 !其次,在选择链接的时候,如<a href="hello"></a>,hello前不能加/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值