springMVC3+JDBCTemplate简单示例(二)

2014-11-24 07:45:49 · 作者: · 浏览: 2
work.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xing.ent.User;
import com.xing.service.UserService;

/**
* guangzhoudaxue
* @author xing
*
*/
@Controller
public class UserController {

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

private UserService userService;
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
logger.info("get all user info ---->/n");
List users = userService.getUserList();
logger.info(users.toString());
model.addAttribute("users", users);

return "showUsers";
}

//注入
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}

}

Step7:配置文件的配置
root-context.xml
[html]
< xml version="1.0" encoding="UTF-8" >
http://www.springframework.org/schema/beans"
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">


class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">





/WEB-INF/application.properties




destroy-method="close">


































[java]

新建一个application.properties文件放置WEB-INF目录下(注意:我使用的是mysql,如果使用其他的数据库可能有点不同,网上很容已找到,或者在spring的sample上都有)

jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/test useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

dbcp.maxIdle=5
dbcp.maxActive=40

servlet-context.xml
这里基本上是没有修改任何东西,添加了一个bean
[html]
< xml version="1.0" encoding="UTF-8" >
http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
























Step8:这是显示界面
showUsers.jsp
[html]
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>

Home


users: ${users}.




以上是我个人学习spring的小小总结,如需转载请注明转载地址,^_^,谢谢各位------一个小小菜鸟,同时也希望各位高手指点下


摘自 baitxttgchx