您的位置:首页 > 编程语言 > Java开发

springMvc的Hello world(xml配置)

2016-03-25 16:17 549 查看
本章主要介绍与Struts2类似的mvc框架SpringMvc,讲解怎么建立第一个helloworld程序的建立

讲解不到位的地方欢迎大家指正:联系方式rlovep.com

详细请看源代码注释:

写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.下载spring的release包

地址:http://repo.springsource.org/libs-release-local/org/springframework/spring/

2.建立一个动态web应用

建立名为springmvc的web应用

导入必须要的包:我使用的是spring4.2,需要导入如下包:

org.springframework.beans-4.2.3.RELEASE.jar

org.springframework.context-4.2.3.RELEASE.jar

org.springframework.core-4.2.3.RELEASE.jar

org.springframework.expression-4.2.3.RELEASE.jar

3.在/WEB-INF/下修改或创建web.xml如下:

<?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" 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">
<display-name>SpringMvc</display-name>
<!-- 注册springMvc核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- 配置映射 -->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 首页配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


4.建立HelloControl.java类并实现Controller接口

package com.rlovep.hello;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloControl implements Controller{

public HelloControl() {
System.out.println("创建helloControl");
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("进入处理方法");
/**
* ModelAndView表示向视图封装的数据和真实路径
*/
ModelAndView modelAndView=new ModelAndView();
//写入一个域对象
modelAndView.addObject("message","HelloWorld");
//真实路径
modelAndView.setViewName("/jsp/hello.jsp");
return modelAndView;
}

}


5.在/webRoot/或者/webContent/下建立jsp/hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一个springMvc程序</title>
</head>
<body>
${message }
</body>
</html>


6.在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件

xml头部信息与spring.xml相同

命名规则:web.xml文件中配置的的值-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<bean name="/hello.action" class="com.rlovep.hello.HelloControl"></bean>
</beans>


7.部署web应用到tomcat中,通过浏览器访问如下URL:http://localhost:8080/springmvc/hello.action

显示如下:



8.注意spring的控制器类是单例的

只会构造一次,但是会重复执行方法,注意与Struts2的区别;



上图的测试可以说明控制器是单例的。

springmvc与struts2主要区别:

1)springmvc的入口是一个servlet,即前端控制器,例如:*.action
struts2入口是一个filter过虑器,即前端过滤器,例如:/*
2)springmvc是基于方法开发,传递参数是通过方法形参,可以设计为单例
struts2是基于类开发,传递参数是通过类的属性,只能设计为多例
3)springmvc通过参数解析器是将request对象内容进行解析成方法形参,将响应数据和页面封装成
ModelAndView对象,最后又将模型数据通过request对象传输到页面
struts采用值栈存储请求和响应的数据,通过OGNL存取数据


好的本章介绍到这里

来自伊豚wpeace(rlovep.com)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: