我们可以从 /WEB-INF/jdbc.properties 中找到这个变量的值. 如果你的数据库已经配置好,就将第一个注释去掉.
jdbc.properties:
1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8
3 jdbc.username=test
4 jdbc.password=12345
现在,我们来编写 Java 代码吧.
1 /***********************************************************
2 * Date: 2006-8-26
3 * File: HelloController.java
4 * Author: ideawu
5 ***********************************************************/
6
7 package com.ideawu;
8
9 import org.springframework.web.servlet.mvc.Controller;
10 import org.springframework.web.servlet.ModelAndView;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 /**
16 * @author ideawu
17 *
18 */
19 public class HelloController implements Controller {
20 /*
21 private HelloManager helloManager;
22
23 public void setHelloManager(HelloManager helloManager) {
24 this.helloManager = helloManager;
25 }
26 */
27
28 public ModelAndView handleRequest(HttpServletRequest request,
29 HttpServletResponse response)throws Exception{
30
31 request.setAttribute("hello_1","你好啊,Spring!");
32 request.setAttribute("hello_2","Hello World!");
33
34 return new ModelAndView("hello");
35 }
36
37 }
return new ModelAndView("hello"); 告诉 InternalResourceViewResolver jsp 模板的名字叫作 hello. request.setAttribute() 设置的对象我们可以在 jsp 文件中使用.
hello.jsp:
1 <%@ page contentType="text/html; charset=UTF-8" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7 <title>Hello World!</title>
8 </head>
9 <body>
10
11 <h2>${hello_1}</h2>
12
13 <h2>${hello_2}</h2>
14
15 </body>
16 </html>
你可以下载整个 Web 应用程序. 在 Debian Linux,Tomcat 5.5.16,JDK1.5.0 下运行良好. 解压后得到一个 spring 文件夹,放到你的 webapps 目录下,在浏览器中输入 http://localhost:8080/spring/hello.do 就可以访问了。 |