程序说明:
在url中访问控制器,在控制器中跳转到jsp页面,完成springmvc控制过程。
第一步:
配置xml
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
说明:<servlet-name>中的spring指在web-inf目录下有spring-servlet.xml文件。
第二步:
在web-inf目录下新建spring-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 加载DefaultAnnotationHandlerMapping和 AnnotationMethodHandlerAdapter--> <mvc:annotation-driven /> <!-- 注释资源扫描包路径 --> <context:component-scan base-package="com.action" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 为return的值加上前缀 --> <!-- <property name="prefix" value="/WEB-INF/jsp/" /> --> <!-- 为return的值加上后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
第三步:
创建action类,HelloworldAction:
package com.action; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloworldAction { @RequestMapping(value = "/helloworld.do") public String show(Model model) { model.addAttribute("helloworld", "helloworld"); return "result"; } }
第四步:
创建result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'result.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${helloworld } <br> </body> </html>
运行结果:
访问:http://localhost:8080/smvc/helloworld.do
结果:在jsp页面中显示 helloworld
相关推荐
刚开始用IDEA学习SpringMVC 的 HelloWorld 实例
SpringMVC跑的一个HelloWorld
Maven+SpringMVC的入门级HelloWorld实例,简单了解Maven和SpringMVC整合
SpringMVC HelloWorld Restfull结构项目。
SpringMVC Helloworld AbstractController 实例
Spring MVC 入门啦,HelloWorld配置是我们掌握这个框架的开始!
SpringMvc的HelloWorld程序, 有兴趣的可以看看, 免费的啊...
SpringMVC HelloWorld工程源码
分享一个springMVC框架入门的demo,希望能够通过这个demo了解springmvc的环境搭建和其原理。
使用springmvc框架实现一个简单表单请求与响应的项目示例
springmvc项目helloworld下载
springmvc 入门开发 helloworld
springMVC输出HelloWorld springMVC输出HelloWorld
这是关于springmvc版的HelloWorld,主要是呈现了spirngmvc处理请求的过程
SpringMVC------从HelloWorld开始
SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- Hello...
使用eclipse 创建基于marven的spring mvc的Helloworld程序,一步一步手把手学会使用spring mvc,共同进步!共同进步!共同进步!共同进步!共同进步!共同进步!
SpringMVC跑Hello World博客相应代码SpringMVC跑Hello World博客相应代码
springmvc注解版每句话都有注释的,希望对你入门有点帮助。记得以前在csdn下载别人的源码要的积分又多,而且还运行不了。所以我上传的源码都会测一遍的,百分之百能跑起来。
笔记:Spring MVC快速开发:第一个例子-Hello World的代码,利用SpringMVC控制页面的一个简单示例