1.新建工程,把spring所需的包copy到工程里
2.配置前端控制器Dispatcher
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.在WEB-INF下创建一个spring mvc的xml配置文件,servletname(springmvc)-servlet.xml(命名规则)
配置handlermapping(可以省略此步)
springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />
<bean name="hello.do" class="zou.huiying.HelloController"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4.创建jsp页面
hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<html>
<head>
<title>this is helloworld</title>
</head>
<body>
<form action="hello.do" method="post">
hello:<input type="text" name="hello" />
<input type="submit" value="tijiao" />
</form>
</body>
</html>
5,创建controller
HelloController.java
package zou.huiying;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String hello=request.getParameter("hello");
System.out.println("---------"+hello);
ModelAndView mav= new ModelAndView("aa");
mav.addObject("helloworld", "nihao"+hello);
System.out.println("---------");
return mav;
// return new ModelAndView(new RedirectView(this.getViewPage()));
}
}
6.指定返回页面
aa.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<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>
This is my JSP page. <br>
<h1>${helloword}</h1>
</body>
</html>
7.配置视图解析器
8.配置controller
上面已经配置好
分享到:
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
基于spring2.5的采用XML配置的spring MVC项目
首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...
Spring MVC 基础实例源码01 我的博客上SpringMVC基础教程 实例的源码
maven spring mvc bubbo 实例
Spring mvc、 Spring、 Spring jdbc 整合实例源码
主要介绍Spring MVC 的使用注解来配置的使用实例
Spring MVC入门实例
IT学习者Spring MVC注解实例.pdf
Spring MVC 实例
SPRING MVC入门实例,适合入门者
spring mvc 实例有数据库,jar包,代码之间还有详细的注解,特别适合初学者学习,之间导入myeclipse工程中就可以用.希望对初学者学习spring mvc框架有帮助.
Spring Mvc实例 主要实现利用spring注解,完成前后台交互,简单的视图控制和数据操作。
Spring mvc整合redis实例(redis连接池) 将所有jedis 增删改进行事务的封装、查询单独做为有返回参数的封装。简化了很多代码
一个动态的java web 工程,使用了spring_mvc框架搭建了一个小小的实例,愿和大家一起共享之,谢谢。希望能帮助到搭建,最近也在打算再次使用spring_mvc框架,却发现网上却少有实例可以提供参考,故而得空自己写了一...
Spring mvc搭建实例。 所需要的jar包是全的、 完美运行
Spring MVC 3实例,包含上传下载,还有Spring mvc jsr303表单验证技术,还有一个spring mvc ajax json等 欢迎下载 自己研究,简单易懂 如果有注解不懂,可以看看...
这是spring MVC的HelloWorld完整实例,包含jar包
spring mvc 登录实例 ,实现简单登录。入门学习
基于Spring mvc的helloworld实例,完整源码打包上传分享大家一起下载学习!