【转载】Spring3 MVC的DEMO

新公司一个新的项目要使用SSH框架实现,可惜我使用Spring已经是N年前的事情了,为了应付新情况,多次GOOGLEBAIDU,终于写出了一个DEMO,记下来,做为一个技术备份吧。

首先去Spring官网上去下载最新的Spring版本。http://www.springsource.com/download/community

目前最新的版本是3.1 M2。为了稳定,我下载了3.0.5.RELEASE。下载完后解压,将dist目录下的所有jar文件复制到你的项目的lib目录下(我在Eclipse中新建了一个myappWeb项目),另外再添加如下的JAR包,

commons-fileupload-1.2.1.jar

commons-logging-1.1.1.jar

然后再web.xml中加入:

<context-param>   

     <param-name>contextConfigLocation</param-name>   

     <param-value>/WEB-INF/applicationContext.xml</param-value>   

 </context-param>   

  <listener>   

     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   

 </listener>   

 

 <servlet>   

     <servlet-name>spring</servlet-name>   

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   

     <load-on-startup>1</load-on-startup>   

 </servlet>   

 <servlet-mapping>   

     <servlet-name>spring</servlet-name>

     <url-pattern>*.do</url-pattern>   

 </servlet-mapping> 

  <filter>   

      <filter-name>Encoding</filter-name>   

      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   

      <init-param>   

        <param-name>encoding</param-name>   

        <param-value>utf8</param-value>   

      </init-param>

    </filter>   

     <filter-mapping>   

        <filter-name>Encoding</filter-name>   

        <url-pattern>/*</url-pattern>   

     </filter-mapping>

另外在WEB-INF下新建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans" 

     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 

     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 

     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-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"> 

     

     <context:annotation-config /> 

     <context:component-scan base-package="org.app.demo.spring" />  <!-- 自动扫描所有注解该路径 -->     

</beans> 

WEB-INF下新建spring-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"   

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

   

  <context:annotation-config /> 

       <!-- 把标记了@Controller注解的类转换为bean -->   

      <context:component-scan base-package="org.app.demo.spring.controller" />

             <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->   

      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />   

      

       <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->   

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"   

          p:prefix="/WEB-INF/views/" p:suffix=".jsp" />   

          

       <bean id="multipartResolver"   

          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"   

          p:defaultEncoding="utf-8" />   

 </beans> 

在源目录下新建三个包

org.app.demo.spring.controller

org.app.demo.spring.service

org.app.demo.spring.service.impl

controller包下建HelloWorldController

package org.app.demo.spring.controller;

 

import javax.servlet.http.HttpServletRequest;

import org.app.demo.spring.service.HelloWorldService;

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.RequestParam;

 

@Controller

@RequestMapping("/helloworld.do")

public class HelloWorldController{

 

       @Autowired

       private HelloWorldService helloWorldService;

      

       @RequestMapping

       public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){

              String newUserName = helloWorldService.getNewName(userName);

              request.setAttribute("newUserName", newUserName);

              return "helloworld";

       }

}

service包下新建HelloWorldService接口

package org.app.demo.spring.service;

public interface HelloWorldService {

       public String getNewName(String userName);

}

impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl

package org.app.demo.spring.service.impl;

import org.app.demo.spring.service.HelloWorldService;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

@Service

public class HelloWorldServiceImpl implements HelloWorldService {

       @Override

       @Transactional

       public String getNewName(String userName) {

              return "Hello Spring!" + userName;

       }

}

新建index.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>

<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">

</head>

<body>

<form action="helloworld.do" method="post">

请输入姓名:<input type="text" name="userName" />

<input type="submit" value="提交" />

<br />

</form>

</body>

</html>

然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.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>

<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">

</head>

<body>

<h1><%=request.getAttribute("newUserName")%></h1>

</body>

</html>

保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp

输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do

显示Hello Spring!张三。一切OK!

posted @ 2013-04-01 09:35  linux,dev  阅读(325)  评论(2编辑  收藏  举报