Spring mvc (一)

来源:互联网 发布:lol for mac 国服 编辑:程序博客网 时间:2024/06/13 23:03

Spring  有自己的一套mvc  很强大

Spring 的下载地址   http://www.springframework.org/download.

 

 下面是一个很简单的Spring mvc框架的一个hello world 程序

1.需要的jar文件有

spring.jar
spring-webmvc.jar
commons-logging.jar
servlet-api.jar

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>    <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 style="font-size:50px;">    HELLO WORLD!  </body></html>

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">  <!-- 配置Springmvc --> <!-- spring-Mvc核心控制器 -->  <servlet>  <!-- 其中的servlet-name 可以自己定义  但是如果是自己定义     创建的spring配置文件的名字必须是   (servlet-name)-servlet.xml               系统会自动加载文件名为这个的xml配置文件  否则自己就需要指定文件名称进行加载      -->    <servlet-name>springapp</servlet-name>    <!-- 核心 -->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet><!-- 访问的文件名是以do结尾的当然 也可以是其他的什么比如    *.jsp/*.htm /*.action ...  -->  <servlet-mapping>    <servlet-name>springapp</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>

   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

springapp-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"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet --> <!-- 添加一个bean入口 指定其一个类作为他的入口 --> <bean  name="/helloAction.do" class="com.dragon.controller.HelloAction">   </bean>

</beans>

controller中的类  名称HelloAction.java名字 是自己起的  因为感觉就像struts2 中action 一样的作用 官方的名字是xxxController写法

package com.dragon.controller;

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

import org.springframework.web.servlet.ModelAndView;

public class HelloAction implements  org.springframework.web.servlet.mvc.Controller {        //创建一个控制器类相当于  struts2 中的   action    public ModelAndView handleRequest(HttpServletRequest request,   HttpServletResponse response) throws Exception {  // TODO Auto-generated method stub  return new ModelAndView("index.jsp"); }

}

 
下来就是部署了 这个都应该会吧  再看一看我的程序结构
Spring mvc (一) - 口袋里的小龙 - 口袋里的小龙
 
在地址栏输入http://localhost:8080/Spring_MVC/helloAction  就可以了  这是一个最最简单的Spring Mvc  通过spring配置文件中bean的名称来访问

原创粉丝点击