`

springMVC 入门

阅读更多

web.xml 简单配置

   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/spring-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>

<!--springMVC 默认解决乱码过滤器配置-->

  <filter>
   <filter-name>springMVCEncoding</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
   <param-name>Encoding</param-name>
   <param-value>UTF-8</param-value>
   </init-param>
    <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
   </init-param>
  </filter>
   <filter-mapping>
   <filter-name>springMVCEncoding</filter-name>
   <url-pattern>/*</url-pattern>
   </filter-mapping>

 

spring-servlet.xml 配置  目录在src 下面的config包中  和web.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:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:lang="http://www.springframework.org/schema/lang"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/jee
 http://www.springframework.org/schema/jee/spring-jee.xsd
 http://www.springframework.org/schema/lang
 http://www.springframework.org/schema/lang/spring-lang.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

 <!-- 开启controller注解支持 -->
 

  <bean name="/test1/helloword" class="com.tgb.web.controller.HelloWordController"></bean>
 
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <!-- 前缀 -->
   <property name="prefix" value="/"/>

<!-- 后缀 -->
  <property name="suffix" value=".jsp"/>
 </bean>


 <!-- 放弃拦截  目录 -->
 <mvc:resources location="/res/" mapping="/res/**"/>
</beans>

HelloWordController.java 文件  在com.tgb.web.controller 包中和spring-servlet.xml 中的路径对上

package com.tgb.web.controller;

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

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWordController implements Controller {
/**
 * 一个Controller 多个方法
 *
 * */
 public ModelAndView handleRequest(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
  // TODO Auto-generated method stub
  System.out.println("hello tgb");
  String hello = "hello spring mvc 中文";
  
  //很明显这里用的是转发  所以 前台取数据  用 request.getAttribute 或者 ${requestScope.} 或者%{}
  Map<Integer,String> map = new HashMap<Integer, String>();
  map.put(1,"中文aa");
  map.put(2,"中文bb");
  map.put(3,"中文cc");
  map.put(4,"中文dd");
  return new ModelAndView("/welcome","map",map);
 }

}

 

welcome.jsp 文件

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'welcome.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>
    
   <p>--------------------------------------</p>
   <table border="1">
   <c:forEach items="${map}" var="m" step="1" begin="0" varStatus="aaa">
    <tr>
    <td>${aaa.index +1}</td>
    <td>${m.key}</td>
    <td>${m.value}</td>
    </tr>
   </c:forEach>
   </table>
   
   
  </body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics