Spring mvc demo 案例 初学者的天堂

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

目前最新的版本是3.1 M2。下载完后解压,将dist目录下的所有jar文件复制到你的项目的lib目录下(我在MyEclipse中新建了一个myapp的Web项目),另外再添加如下的JAR包,
commons-fileupload-1.2.1.jar

commons-logging-1.1.1.jar

在web. xml中添加:


		contextConfigLocation
		/WEB-INF/applicationContext.xml
	
	
		
			org.springframework.web.context.ContextLoaderListener
		
	
	
		spring
		
			org.springframework.web.servlet.DispatcherServlet
		
		1
	
	
		spring
		*.do
	
	
		Encoding
		
			org.springframework.web.filter.CharacterEncodingFilter
		
		
			encoding
			utf8
		
	
	
		Encoding
		/*
	


 

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

 

      

      

       

            

  


 

在WEB-INF下新建spring-servlet.xml

  

    

    

    

           

      

                 

          

       

           

           

          

           

   


 

在源目录下新建三个包

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









Insert title here







请输入姓名:


然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Insert title here







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


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

输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK!

 

 

 

你可能感兴趣的:(J2EE)