`
a942010
  • 浏览: 1463 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

自己学的关于springMVC的简单实现(基于注解)

 
阅读更多

首先是Spring的配置
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>base</display-name> 
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
classpath*:spring-config.xml
    </param-value>
  </context-param>
    <!-- 配置 springMVC-->   
     <servlet> 
        <servlet-name>springMVC</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param> 
          <param-name>contextConfigLocation</param-name> 
          <param-value>classpath:springMVCconfig/springMVC-servlet.xml</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
  </servlet> 
   <servlet-mapping> 
    <servlet-name>springMVC</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping> 
  <!-- end 配置spring MVC --> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>

contextConfigLocation 是上下文初始化配置
classpath是类路径 是classes下包括所有的文件路径。
spring-config一般是配置数据库连接,事务 信息


web.xml最重要的是配置DispatcherServlet  因为这是所有访问路径的入口。DispatcherServlet  继承自HttpServlet,访问Url时,映射到访问的方法,该方法执行相应的操作后,返回一个视图。


到配置servlet文件  文件名是web.xml中<servlet-name>-servlet.xml即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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
    default-autowire="byName"> 
<!-- 启动注解-->
    <mvc:annotation-driven /> 
     
    <!-- start开启注释  springMVC.controller包下的类使用注解--> 
    <context:component-scan base-package="springMVC.controller"> 
    </context:component-scan> 
    <!-- end开始注释 --> 
     
     
    <!-- start视图配置  /WEB-INF/jsp/ 前缀 .jsp 后缀 --> 
    <bean 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 
    <!-- end视图配置 --> 
</beans> 


我在这里用的是spring3.0版本。

如果返回的视图名是hello  那么spring会去找WEB-INF/jsp/hello.jsp这个文件。不配做视图,默认是在WEB-INF下的



下面是Controller代码


package springMVC.controller;

import java.util.HashMap;
import java.util.Map;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

@RequestMapping("/hello")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("进入Controller");
Map<String,String> map=new HashMap<String,String>();
map.put("map1", "我很好啊");
map.put("map2", "value2");
String result="good study!!";

return new ModelAndView("hello","map",map);
}

}


返回的hello.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>大家好!!hdlkasjkl</h1><br/>
${map.map1 }
</body>
</html>

因为给视图传了一个名为map的map集合,可以通过map.key来获取value

 

 

最后返回的页面

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics