Spring Web MVC 入门实例
2008-06-26 10:50
405 查看
今天学习了Spring的MVC.根据 http://dev.yesky.com/238/2599738.shtml做了个例子.
Spring 的web MVC 框架中担任前端控制器叫色的是org.springframework.web.servlet.DispatcherServlet,DispatcherServlet负责将客户的请求分配给控制对象,所以使用Spring Web MVC的第一步,就是在web.xml中定义DispatcherServlet;
Spring 的web MVC 框架中担任前端控制器叫色的是org.springframework.web.servlet.DispatcherServlet,DispatcherServlet负责将客户的请求分配给控制对象,所以使用Spring Web MVC的第一步,就是在web.xml中定义DispatcherServlet;
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>ntx</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/ntx.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ntx</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
在web.xml中定义了一个DispatcherServlet的实例ntx,从设定中可以看到,所有连接至*.do结尾的请求都会由它来处理,"contextConfigLocation"初始化参数用来设定Bean定义文件的 位置与名称,如果不设置,则DispatcherServlet默认会使用Servlet的名称为前置,读取"Servlet 名称_servlet.xml"作为其Bean定义文件,在上面的设定中则读取nxt.xml中的定 义。 DispatcherServlet负责分配请求至控制对象(Controller),在Spring Eeb MVC框架中,控制对象要实现org.springframework.web.servlet.mvc.Controller接口,Controller接口有一个必须实现的handleRequest()方法,其定义如下: package com.wisetop.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.wisetop.service.LoginService; public class LoginController implements Controller { private LoginService loginService; private String gotoUrl; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String userName = request.getParameter("userName"); this.getUserInfo(request, userName); return new ModelAndView(this.getGotoUrl()); } private void getUserInfo(HttpServletRequest request, String |userName) { String userInfo = this.getLoginService().getUserInfo (userName); request.setAttribute("userInfo", userInfo); } public String getGotoUrl() { return gotoUrl; } public void setGotoUrl(String gotoUrl) { this.gotoUrl = gotoUrl; } public LoginService getLoginService() { return loginService; } public void setLoginService(LoginService loginService) { this.loginService = loginService; } } 其他两个相关的接口和类定义如下: package com.wisetop.service; public interface LoginService { public String getUserInfo(String userName); } package com.wisetop.serviceimpl; import com.wisetop.service.LoginService; public class LoginServiceImpl implements LoginService { public String getUserInfo(String userName) { return "你的名字是" + userName; } }
Bean文件定义如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="no" default-lazy-init="false" default-dependency-check="none"> <bean id="loginService" class="com.wisetop.serviceimpl.LoginServiceImpl" /> <bean id="loginController" class="com.wisetop.controller.LoginController"> <property name="loginService"> <ref bean="loginService" /> </property> <property name="gotoUrl"> <value>/showResult.jsp</value> </property> </bean> <bean id="SimpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandler|Mapping"> <property name="mappings"> <props> <prop key="/userLogin.do">loginController</prop> </props> </property> </bean> </beans>
测试页面:index.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <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> <div> <form method="post" action="userLogin.do"> <input type="text" name="userName" size="30"/><br/> <input type="submit" value="提交 "/> <a href="userLogin.do?userName=1">fffff</a> </form> </div> </body> </html> showResult.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <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> <% String a=(String)request.getAttribute("userInfo"); %> <%=a%> </body> </html>
相关文章推荐
- Spring-MVC入门:入门实例
- SpringMVC+Spring+MyBatis整合完整版Web实例(附数据)
- 手把手 Spring4 MVC入门之HelloWord实例
- 【第二章 Spring MVC入门】Spring Web MVC核心架构
- Spring-MVC入门(一):入门实例 (重点学习)
- webuploader在springMVC+jquery+Java开发环境下的大文件分片上传的实例代码
- Java - SpringMVC+iBatis maven工程项目增删改查实例入门+源码
- Spring+SpringMVC+Mybatis 两种入门级基本配置及登录实例
- 第一章:Spring Web MVC入门——深入浅出学Spring Web MVC
- 第一章:Spring Web MVC入门——深入浅出学Spring Web MVC
- 第一章:Spring Web MVC入门——深入浅出学Spring Web MVC
- 第一章:Spring Web MVC入门——深入浅出学Spring Web MVC
- springmvc+spring jdbc+velocity入门教程及其代码实例
- spring Web MVC入门
- MyBatis代码实例系列-05:Mybatis初步整合Spring + Spring MVC框架,实现Web请求实例
- Spring3 MVC入门实例(示例)Spring3 MVC教程
- Spring 4 MVC入门实例
- spring-mvc入门(一):入门实例
- Spring3 MVC入门实例(示例)Spring3 MVC教程
- Webcollector + Spring + MVC 搭建应用初探(六)(Lenskit 推荐系统实例)