love li fang fang forever

SSH 三层架构中struts 实现连接数据库Mysql代码参考

• _posts

迁移源地址为:《SSH 三层架构中struts 实现连接数据库Mysql代码参考》

date:2015.04.10

author:Chee \ bgwan

declare:Reproduced original sources 转载标明出处

1.index.jsp

<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8”%> <% String path = request.getContextPath(); String basePath = request.getScheme() + “://”

  • request.getServerName() + “:” + request.getServerPort()
  • path + “/”; %> <%@ taglib uri=”/struts-tags” prefix=”s”%> <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
Demo_iterate This is my JSP page.
---------------------------
<% List list = new ArrayList(); list.add("Leon"); list.add("John"); list.add("Peter"); list.add("Jeff"); list.add("Linda"); request.setAttribute("names", list); %>

Names:



---------------------------
  1. 奇数:
  2. 偶数:
---------------------------


---------------------------
abc在集合 abc不在集合
---------------------------
</body> </html> 2.welcome.jsp 同时也是struts基本标签的用法 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> My JSP 'welcome.jsp' starting page This is my JSP page.
亲爱的用户欢迎您: 下面是struts2一般标签的测试内容:

----111111111--------s:include-----------


---222222222---------s:param-and s:include----------


-----33333333-------s:property--and push---------

param的作用是:为其他标签如include,bean提供参数

下面没有提供value参数:为默认值

--4444444444----------s:set标签-----------


------555555555------s:text标签-----------



Mr Smith

------6666666666------s:if s:elseif s:else标签-----------

cuowu111

正确222

haha333

------77777777777------iterator标签-----------

<% List list = new ArrayList(); list.add("Leon"); list.add("John"); list.add("Peter"); list.add("Jeff"); list.add("Linda"); request.setAttribute("names", list); %>

Names:



---------------------------
  1. 奇数:
  2. 偶数:

--8888888888----------sort标签-----------


----99999999999--------data标签-----------

<%Date now = new Date(); pageContext.setAttribute("now", now);%>nice=f format
nice=t format
no format nice =t
no format nice=f

----1010101010--------il18标签-----------


-----0000000000000-------url标签-----------

添加 编辑 删除 <a href=" ">删除2</a>
</body> </html> 3.fail.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> My JSP 'fail.jsp' starting page This is my JSP page.
对不起 亲爱的 您输入的账号或密码错误:
这也是一个include标签插入页: 下面为接受的数据

${param.author }

${param.author } 4.MyComarator.java package com.ascen.util; import java.util.Comparator; public class MyComarator implements Comparator{ @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub return o1.toString().length()-o2.toString().length(); } } 5.Usr.java package com.ascent.po; public class Usr { private String username; private String password; 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; } } 6.LoginAction.java package sunshuntao.loginAction; import sunshuntao.loginModel.LoginModel; public class LoginAction { private String username; private String password; 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; } public String execute(){ String result = "fail"; LoginModel u = new LoginModel(); boolean b = u.login(username,password); if(b){ result="succ"; } return result; } } 7.LoginModel.java package sunshuntao.loginModel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class LoginModel { public boolean login(String username, String password) { // TODO Auto-generated method stub System.out.println("用户名:"+username); System.out.println("密码:"+password); boolean r = false; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/jw"; Connection conn = DriverManager.getConnection (url, "root", "123456"); Statement stat = conn.createStatement(); String sql = "select *from userinfo where username='" + username + " 'and password='" + password + "'"; ResultSet resu = stat.executeQuery(sql); if (resu.next()) r = true; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return r; } } 8.struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> /fail.jsp /welcome.jsp 9.web.xml Demo_iterator struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter actionPackages annotation.actions struts2 /* 10.数据库创建和链接机制参考相关资料。 备注:项目实现的功能比较多,不太适合初学者。struts 架构 、struts链接数据库mysql、struts一般标签的使用(下一遍日志会记录)、简单的体现了SSH的一种开发模式。