码迷,mamicode.com
首页 > 编程语言 > 详细

搭建一个springMVC项目以及遇到的问题

时间:2016-02-20 16:07:22      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

首先找到jar包(lz现在还在学习maven,以后回了,就用maven了,自己配置时,jar包不全就很容易到时搭建失败)

技术分享

如果缺少jar包,可能出现的问题,在tomcat启动期间就报错了

第二,配置web.xml

首先还是看一下文件结构

技术分享

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
    </context-param>
    
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
        
</web-app>
 

这里没有log4j和其他的配置,只是一个简单的Spring的环境的搭建

第三,springMVC-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd     
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描的包 -->
    <!-- 为了测试app07先注释app04a和app05a -->
    <context:component-scan base-package="app07a.controller"/>
      <mvc:annotation-driven ></mvc:annotation-driven>
    <!-- 这个的作用是让DispatcherServlet不将下列路径理解为一个request请求,
    在项目中,这个是必须的,如果没有加这些就可能造成上述问题 -->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/*.html" location="/"/>
        
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

这里需要注意的几点

<mvc:annotation-driven ></mvc:annotation-driven>
这句是为了让spring启用识别注解的功能
<context:component-scan base-package="app07a.controller"/>
这句是为了让spring扫描以app07a.controller开头的包
第四:在controller中一个简单的action,
package app07a.controller;

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

@Controller
public class testController
{
    @RequestMapping(value="/test")
    public String test()
    {
        System.out.println("test into");
        return "test";
    }

}

注意:这里 return "test";其实是指向了一个叫test.jsp的文件,这个在springmvc-servlet中有写,他的父路径也在配置文件中找到



 

搭建一个springMVC项目以及遇到的问题

标签:

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/5203370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!