班级: 姓名:
实验时间:2012年06月12日 指导教师:
一、实验目的
1.了解数据库连接池。 2.掌握Spring应用。 3.掌握Hibernate应用。
二、实验内容
1. 创建一个基于Spring MVC的Web应用项目。 2. 实现项目的国际化处理。 3. 实现用户的登录、注册处理。
4. 编写一个小型留言板系统,利用Hibernate实现留言板处理。 三、操作步骤
㈠、基于Spring MVC实现用户注册和登录 项目整体框架: 项目web4
包 包 图形
controllers counts 界面
㈡、主要代码: User.java
package counts;
public class User { private int id;
private String username; private String password; public User(){}
public User(int id, String username, String password) { super();
this.id = id;
this.username = username; this.password = password;
}
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
UserDao.java
package counts;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
public class UserDao {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; }
public void insert(final User user) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { String sql = \"insert into user(username,password) values(?,?)\"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); return ps; } }); }
public List return list; } } 控制层代码: Login.java package controllers; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import counts.User; import counts.UserDao; @WebServlet(\"/login.do\") public class Login extends HttpServlet { private static final long serialVersionUID = 1L; public Login() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter(\"username\"); String password = request.getParameter(\"password\"); UserDao userDao = new UserDao(); List } } rd = request.getRequestDispatcher(\"WEB-INF/admin.jsp\"); else { rd = request.getRequestDispatcher(\"login.jsp\"); } request.setAttribute(\"users\rd.forward(request, response); } catch (Exception e) { e.printStackTrace(); } Register.java package controllers; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import counts.User; import counts.UserDao; @WebServlet(\"/register.do\") public class Register extends HttpServlet { private static final long serialVersionUID = 1L; public Register() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(\"utf-8\"); ApplicationContext context = new ClassPathXmlApplicationContext( \"beans.xml\"); UserDao register = (UserDao) context.getBean(\"userDao\"); User user = new User(); user.setUsername(request.getParameter(\"username\")); System.out.print(user.getUsername()); user.setPassword(request.getParameter(\"password\")); register.insert(user); response.setContentType(\"text/html;charset=utf-8\"); response.getWriter().println(\"注册成功!\"); } } 视图层代码: Login.jsp <%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%> <%@taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\"%> 登录失败! Register.jsp <%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%> <%@taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\"%> 注册失败! Admin.jsp <%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%> 配置文件bean Beans.xml 运行结果: ㈢、国际化处理: 主要代码: Test.java package test; import java.util.Locale; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext (\"config.xml\"); String str = ctx.getMessage(\"name\;//new Locale(\"\")); System.out.println(str); } } 配置文件config.xml messages_zh_CN.properties name=\你\好\,\朋\友\! Messages.properties name=hello,Friend! 中文:String str = ctx.getMessage(\"name\; 英文:String str = ctx.getMessage(\"name\ 运行效果: 四、实验收获和建议 通过学习首先我觉得Java 的基础、面向对象的基础和设计模式的基础知识是必不可少的。我在java方面的只是储备还很不足,在理解一些代码的时候有些吃力,自己还不能写出一个完整的程序代码,所以我恶补了java知识,有了这些基础,在理解方面没有太大的问题了,简单的程序代码都可以看得懂了。有了spring框架之后,可以很方便的做一个简单的完整的程序,但是有些东西还是不能很好的理解。通过做项目,遇到了很多的问题,一个小小的问题自己搞了几天,结果还不是很乐观,有时候还越高越乱。总之这个过程还是比较辛苦的。不过出了成果之后,自己还是比较兴奋的,学习过程当中有苦有乐。 建议: 我觉得首先不能太懒,要勤动手,通过自己做项目遇到问题解决问题学习; 恶补java等一些基础知识; 遇到问题首先自己想办法解决,大胆的尝试各种方法,自己实在解决不了的问题请教别人; 因篇幅问题不能全部显示,请点此查看更多更全内容