Spring mvc入门程序
2016-10-05 11:18
260 查看
Spring mvc入门程序
Spring mvc入门程序实例
实例
首先要导入Spring mvc IOC和Spring web的jar包配置核心控制器,/Spring_mvc_Test1/WebRoot/WEB-INF/DispatcherServlet-servlet.xml,该配置文件名字必须是DispatcherServlet-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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> </beans>
在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_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 注册Spring mvc核心控制器 --> <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> </web-app>
创建/Spring_mvc_Test1/src/com/jpzhutech/action/HelloAction.java
package com.jpzhutech.action; 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 HelloAction implements Controller{ //业务方法 @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //modelAndView表示向视图封装的数据和路径 ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "这是我的第一个spring mvc的应用程序"); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } }
写一个success.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 成功的跳转到jsp页面<br /> ${requestScope.message}<br /> </body> </html>
最后在DispatcherServlet-servlet.xml中配置bean
<?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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 注册Action class表示文件的全名 name表示请求路径 --> <bean name="/hello.action" class="com.jpzhutech.action.HelloAction"></bean> </beans>
将上述配置文件DispatcherServlet-servlet.xml改造成mvc模式
<?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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 注册Action class表示文件的全名 name表示请求路径 这里的部分是必写的 程序员实现的action必须要将其放进Spring的IOC容器中 --> <bean name="/hello.action" class="com.jpzhutech.action.HelloAction"></bean> <!-- 映射器handler org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping表示将上述bean标签的name属性当成url请求 可选 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 适配器 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter表示用于寻找实现了Controller接口的Action类 可选 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 视图解析器 org.springframework.web.servlet.view.InternalResourceViewResolver表示通过ModelAndView对象中封装的视图名找到真正的页面 可选 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
相关文章推荐
- spring MVC 入门程序
- Spring MVC优势和第一个入门程序
- Spring入门17 - 第一个Spring MVC Web程序
- spring MVC 入门程序
- spring mvc 入门程序
- 搭建Spring MVC 入门程序
- windows程序与vc消息处理入门概念
- 入门训练:写程序应注意的事项
- 第二周学习计划:继续学java入门,开始做些小jsp+java程序
- WIN32 编程示例:嵌入式式软件开发程序入门(完全从网上copy的文章)
- WinCE嵌入式开发程序入门
- 我的程序人生(一)--无意的入门
- ASP编程入门进阶(三):接触脚本程序
- Spring研究 (1) 第一个Spring MVC程序
- javaRMI使用入门程序及配置
- ASP小偷(远程数据获取)程序的入门教程
- GBA程序开发入门1
- GBA程序开发入门2
- COM程序编写入门(全文-3)
- vc入门宝典十二(调用其它程序)