Sping-servlet.xml mvc配置文件
1) spring-servlet.xml mvc的配置文件如下:
<?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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            
             
            
             
            
             
            
             
            
             
            ">
     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
     <mvc:annotation-driven /> 
     <!-- 要映射的包名,表示这里所定义的包名,全都被可以被注释使用。 --> 
 <context:component-scan base-package="com.zy.ywyd.gameoperators.action" />
    <context:component-scan base-package="com.zy.ywyd.gameoperators.service" />
    <context:component-scan base-package="com.zy.ywyd.gameoperators.dao" />
 <!-- 完成请求和注解POJO的映射 -->
    <bean />
 <bean p:prefix="" p:suffix=".jsp" /> <!—定义了返回路径,以及返回文件的后缀。-->
</beans>
配置完成之后, spring 的mvc 的配置就差不多完成了,剩下的就是写一个jsp测试界面和action的类了。
测试mvc的代码:
Jsp测试代码
Jsp:
<%@ page language="Java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<html>
<head>
<%
 String path = request.getScheme() + "://" + request.getServerName()
   + ":" + request.getServerPort() + request.getContextPath()
   + "/";
%>
<base href="https://www.linuxidc.com/<%=path%>" />
 <title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
</head>
<body>
 <a href="https://www.linuxidc.com/TestAction/helloWorld.html">Say Hello</a>
 <form method="POST" action="https://www.linuxidc.com/TestAction/helloWorld.html">
  <input type="text" value="321">
  <input type="submit">
 </form>
</body>
</html>
代码很简单,这里提交主要有两种方式,一个是get 一种是post两种。感觉应该包含了大部分的要求了。
Java代码:
package com.zy.ywyd.gameoperators.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.zy.ywyd.gameoperators.service.TestService;
@Controller
@RequestMapping("TestAction")
public class TestAction {
 @Autowired
 private TestService testS;
 @RequestMapping(value="helloWorld", method=RequestMethod.POST)
 public void helloWorld(HttpServletRequest request,HttpServletResponse response,
   @RequestParam String name) {
  String message = "Hello World, Spring 3.0!";
  System.out.println(testS.test());
  System.out.println(message);
  System.out.println(name);
  try {
   response.getWriter().write("11111");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
//  return new ModelAndView("/jsp/hello", "message", message);
 }
}
