springMVC的HelloWorld:如此简单暴力易上手

springMVC.jpg

学习SpringMVC之前,我们要先对SpringMVC有一个大概的了解
<a href="http://baike.baidu.com/link?url=4Qbm9fPcyjpDzOe2qfu6xHfG92OGf5GlzWwFM_-B4AAnJJor5Bkwbskv3fW4Y6GmpLQTNCeeDR9sUyc16iVrwXF32be5QvCGPAu0nW_Nwqm">SpringMVC的介绍</a>


第一个程序

1.1 新建一个动态的web项目


动态项目.png

1.2 修改classpath路径
web项目默认去WEB-INF寻找classes,如果出现什么配置文件找不到,首先考虑的就是这个classes设置没有
在WEB-INF里面创建一个classes的字节码文件夹


修改SpringMVC项目的classes路径.png

2.导入相关的jar包
有些包后面也会用到的,所以我一起截图了
jar包.png

3.导入对应的配置文件,和spring的配置文件一样.类似于Struts.xml
建议新建一个源文件夹resources,将配置文件放在该目录下面


resources源文件夹.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
</beans>

4.1 编写核心类HelloWorldController

//实现org.springframework.web.servlet.mvc.Controller接口
public class HelloWorldController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        System.out.println("HelloWorldController.handleRequest()");
        return null;
    }
}

4.2 回到applicationController.xml里面添加对应bean的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
        <bean name="/helloWorld" class="com.jianshu.springmvc.HelloWorldController"></bean>
</beans>
name 前台以什么路径访问我们的处理类
class 请求过来后的处理类

5.配置前端请求分发器(DispatcherServlet).在web.xml里面配置Servlet
因为请求分发器底层就是一个Servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">

    <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:applicationContext.xml</param-value>
    </init-param>
    <!-- 启动级别 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
<load-on-startup>1</load-on-startup>
 默认是第一次访问的时候才去加载配置,
但是第一个用户访问的时候,响应特别慢,希望在服务器启动的时候就加载

<init-param>
在SpringMVC默认去WEB-INF文件夹下寻找配置文件
需要告诉告诉框架去指定位置寻找配置文件
contextConfigLocation是DispatcherServlet类里面的变量
如果出现
Could not open ServletContext resource异常
就是没有配置这个

url-pattern  路径

6.去tomcat目录下面的server.xml文件,添加相关的配置

<Context path=""  docBase="F:\Mark_workspace\sprinMVC_test\WebContent"/>

7.启动tomcat ,在浏览器输入地址

因为我的端口设置为80了,所以我在页面上直接输入localhost/helloWorld
如果端口号是默认,那么请输入localost:8080/helloWorld

之后可以在控制台看到打印了语句
HelloWorldController.handleRequest()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容