手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring MVC 注释 + iBATIS 整合

[日期:2014-01-01] 来源:Linux社区  作者:chenlong [字体:大 中 小]

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
            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/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-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/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 启动注解驱动的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 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 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="<%=path%>" />
 <title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
</head>
<body>
 <a href="TestAction/helloWorld.html">Say Hello</a>
 <form method="POST" action="TestAction/helloWorld.html" id="test">
  <input type="text" name="name" id="name" 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);
 }
}

linux
相关资讯       iBATIS  Spring MVC 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款