基于annotation的SpringMVC入门小例子

这几天一直在学SpringMVC,第一次参照网上的例子,直接把SpringMVC基于xml的入门搞定了,但是在用annotation的时候纠结了蛮久的,才搞好,啥也不多说了,上代码。首先是web.xml的:  

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/log4j.properties</param-value>
	</context-param>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
    </context-param>  
     <listener>     
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
 </listener>     
</web-app>

 简单的说明一下,dispatcherServlet就不多说了,整个SpringMVC action的入口。log4jConfigLocation是log4j的配置文件,我这里用的是

网上的模板,内容很多,大家可以自己搜,或者我另外开文发出来。contextConfigLocation是用于ContextLoaderListener的

参数,为了能够读取applicationContext.xml。

接下来是dispatcherServlet-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="com.yanbo.controller" />
	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	 
	
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/view/" p:suffix=".jsp" />

	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="utf-8" />
</beans>  

   内容都在注释里,这里声明,这个是参考别人的配置,在此多谢很多前辈。

然后是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="com.yanbo" />  <!-- 自动扫描所有注解该路径 -->
</beans>

  然后是java代码:

首先是controller的:

package com.yanbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yanbo.service.IUserService;

@Controller
public class UserController {
	@Autowired(required=true)
	private IUserService userService;
	@RequestMapping("/user.do")
	public String load(ModelMap modelMap) {
		modelMap.put("user", userService.loadUser());
		return "student";
	}
}

 这里使用了@controller,省了很多代码和配置文件,非常简洁,使用了@Autowired进行bytype的自动注入,@RequestMapping("/user.do")是说明这个方法处理user.do这个请求,使用起来很方便吧

然后是service:

接口:

package com.yanbo.service;

import java.util.List;

import com.yanbo.pojo.User;

public interface IUserService {
	public List<User> loadUser();
} 

  实现:

package com.yanbo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yanbo.dao.IUserDao;
import com.yanbo.pojo.User;
@Service
public class UserService implements IUserService {
	@Autowired(required=true) 
	private IUserDao userDao;

	public List<User> loadUser() {
		return userDao.loadUser();
	}

}
 

最后是dao:

接口:

package com.yanbo.dao;
import java.util.List;

import com.yanbo.pojo.User;

public interface IUserDao {
	public List<User> loadUser();
}

实现:

package com.yanbo.dao;

import java.util.ArrayList;
import java.util.List;


import org.springframework.stereotype.Repository;

import com.yanbo.pojo.User;
@Repository
public class UserDao implements IUserDao {

	public List<User> loadUser() {
		// TODO Auto-generated method stubi
		List<User> list=new ArrayList<User>();
		User user=new User();
		user.setId(0);
		user.setName("yanbo");
		list.add(user);
		return list;
	}

}

pojo:

package com.yanbo.pojo;

public class User {
	public String name;
	public long id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

}
 

只是随便模拟一下,就不搞数据库了。

然后是页面,放在webb-inf的view文件夹下,student.jsp:

<%@ page language="java" import="java.util.*,com.yanbo.pojo.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
    <%List<User> l=(List<User>)request.getAttribute("user");
    for(User u:l)
    {
    out.print(u.getId()+" "+u.getName());
    }
     %>
  </body>
</html>

 搞定收工。基本上没有怎么讲解基础知识,只是写了个小例子,希望对大家有所帮助。最后提交下源码:

猜你喜欢

转载自yanbo.iteye.com/blog/1130995