天天看點

從零開始建立一個簡單的springMVC工程(包含一些異常的解決方法)

目标:

整合一些基本的springmvc的jar包,并进行基本的配置,在浏览器中输出hello.jsp中的内容

 

1.在eclipse中新建一个动态的WEB工程;

2.将以下基础包导入工程中;

 

從零開始建立一個簡單的springMVC工程(包含一些異常的解決方法)

3.在WebContent/WEB-INF/目录下面新建一个web.xml文件,在文件中输入以下内容:

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appxmlns="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">

    <!-- MVC Servlet,配置DispatcherServlet-->

    <servlet>

       <servlet-name>dispatcher</servlet-name>

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

        <!-- 设置配置文件的路径-->

        <init-param>

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

           <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

        </init-param>

        <!-- 表示启动容器时初始化该Servlet;-->

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

 
    <servlet-mapping>

       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
           

4.在WebContent/WEB-INF/目录下面新建一个dispatcher-servlet.xml文件(该文件名与上文配置中的一致),文件内输入如下内容:

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                   


    <!-- scan the package and the subpackage -->

    <context:component-scanbase-package="com.whut"/>

    <!-- don't handle the static resource-->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you mustconfigure following setting -->
    <mvc:annotation-driven />  

    <!-- configure theInternalResourceViewResolver -->
    <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix"value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix"value=".jsp" />
    </bean>
</beans>
           

5.根据上面的配置,我们的控制组件应该放在包名为com.whut的包下,在该包下建立一个控制器

内容为:

packagecom.whut;

importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;


@Controller
publicclass helloController {

 @RequestMapping("/hello")
    public String hello(){       
        return"hello";
    }
}
           

6.还是根据上面的配置,jsp格式的视图文件应该放在WebContent/WEB-INF/jsp目录下,因此在WebContent/WEB-INF/目录下新建一个名为jsp的文件夹,并创建一个hello.jsp文件

内容为:

<%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPEhtml PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Inserttitle here</title>
</head>
<body>

<h1>Hello! My first springMVC</h1>

</body>
</html>
           

7.通过eclipse编译并部署项目到tomcat上,在浏览器中输入localhost:8080/mySpringMVC/hello就显示了hello.jsp的页面

可能出现的异常

异常1:在通过eclipse部署工程时,可能会出现8080端口被占用,而无法启动的情况,“Several ports (8080, 8009) required by Tomcat v6.0Server at localhost are already in use.”。

解决方法:重启电脑可以解决80%的问题,这个也不例外,当时总是重启太过麻烦,还有个比较简单的方法:

1.以管理员身份运行cmd(命令提示符);

2.输入“netstat  -ano|findstr 8080” 查看占用8080端口的进程;

3.查看到进程后,用命“taskkill  /pid进程号 /f”将占用的进程砍掉就好了(进程号可以在上一个命令执行后会出现结果中找到)

 

 

异常2:还是在部署工程时,tomcat成功打开了,但是无法访问目标页面,并可能报出505错误,提示:“Error instantiating servlet classorg.springframework.web.servlet.Dispatcher”,也就是说找不到Dispatcher这个类。

解决方法:

1.检查Dispatcher所在的包是否已经导入工程,本例中对应“spring-webmvc-4.0.4”这个包;

2.如果包导入了,那就选中工程,点击右键的“reflash”将工程刷新一下就好了;

一般以上两步就解决问题了,若还是不行,建议继续百度。

 

 

参考资料:

http://www.admin10000.com/document/6436.html

http://jingyan.baidu.com/article/1612d5006c3cdae20e1eee04.html

http://bbs.csdn.net/topics/390416204

繼續閱讀