Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256012
  • 博文数量: 45
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 488
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-13 14:43
文章分类

全部博文(45)

文章存档

2014年(2)

2013年(35)

2012年(8)

我的朋友

分类: Java

2013-12-17 17:34:17


点击(此处)折叠或打开

  1. package cn.edu.shou.hsh.control;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;

  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.Controller;

  8. import cn.edu.shou.hsh.DAO.UserDAO;
  9. import cn.edu.shou.hsh.Impl.UserImpl;
  10. import cn.edu.shou.hsh.model.User;

  11. public class HelloWorldController implements Controller {
  12.     
  13.     private User user;
  14.     private UserDAO userDAO;
  15.     
  16.     
  17.     public UserDAO getUserDAO() {
  18.         return userDAO;
  19.     }
  20.     public void setUserDAO(UserDAO userDAO) {
  21.         this.userDAO = userDAO;
  22.     }
  23.     public User getUser() {
  24.         return user;
  25.     }
  26.     public void setUser(User user) {
  27.         this.user = user;
  28.     }
  29.     public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
  30.         
  31.         System.out.println("hsh");
  32.         user.setUsername(arg0.getParameter("username"));
  33.         user.setPassword(arg0.getParameter("password"));
  34.         String address=userDAO.checkUser(user);
  35.         
  36.         if("error".equals(address)){
  37.             return new ModelAndView("/error");
  38.         }else{
  39.             Map<String,Object> map = new HashMap<String,Object>();
  40.             map.put("用户:", arg0.getParameter("username"));
  41.             map.put("地址:", address);
  42.             return new ModelAndView("/welcome","map",map);
  43.         }
  44.         
  45.         
  46.         
  47.     }

  48.     
  49.     
  50. }

点击(此处)折叠或打开

  1. package cn.edu.shou.hsh.DAO;

  2. import cn.edu.shou.hsh.model.User;

  3. public interface UserDAO {
  4.     public String checkUser(User user);
  5. }


点击(此处)折叠或打开

  1. package cn.edu.shou.hsh.Impl;

  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.sql.SQLException;

  5. import com.ibatis.common.resources.Resources;
  6. import com.ibatis.sqlmap.client.SqlMapClient;
  7. import com.ibatis.sqlmap.client.SqlMapClientBuilder;

  8. import cn.edu.shou.hsh.DAO.UserDAO;
  9. import cn.edu.shou.hsh.model.User;

  10. public class UserImpl implements UserDAO {

  11.     private static SqlMapClient sqlMapClient = null;
  12.     // 读取配置文件
  13.     static {
  14.         try {
  15.             Reader reader = Resources.getResourceAsReader("config/sqlMapConfig.xml");
  16.             sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
  17.             reader.close();
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         }
  21.     }
  22.     public String checkUser(User user) {
  23.         // TODO Auto-generated method stub
  24.         Object u = null;
  25.         System.out.println("hsh:"+user.getUsername());
  26.         try {
  27.             u = sqlMapClient.queryForObject("selectUser",user);
  28.         } catch (SQLException e) {
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         }
  32.         System.out.println("end search:"+u);
  33.         if (u != null) {
  34.             return u.toString();
  35.         } else
  36.             return "error";
  37.         
  38.     }

  39. }

点击(此处)折叠或打开

  1. package cn.edu.shou.hsh.model;

  2. public class User {

  3.     private int id;
  4.     private String username;
  5.     private String password;
  6.     private String address;
  7.     public int getId() {
  8.         return id;
  9.     }
  10.     public void setId(int id) {
  11.         this.id = id;
  12.     }
  13.     public String getUsername() {
  14.         return username;
  15.     }
  16.     public void setUsername(String username) {
  17.         this.username = username;
  18.     }
  19.     public String getPassword() {
  20.         return password;
  21.     }
  22.     public void setPassword(String password) {
  23.         this.password = password;
  24.     }
  25.     public String getAddress() {
  26.         return address;
  27.     }
  28.     public void setAddress(String address) {
  29.         this.address = address;
  30.     }

  31. }


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.     <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  3.     "">
  4. <sqlMap>
  5.     <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
  6.     <typeAlias alias="User" type="cn.edu.shou.hsh.model.User"/>
  7.  
  8.     <select id="selectUser" parameterClass="User" resultClass="String">
  9.         select address from Admin where username = #username# and password=#password#
  10.     </select>
  11.     
  12. </sqlMap>


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.  xmlns:context=""
  4.  xmlns:p=""
  5.  xmlns:mvc=""
  6.  xmlns:xsi=""
  7.  xsi:schemaLocation="
  8.       /spring-beans-3.0.xsd
  9.       
  10.       /spring-context.xsd
  11.       
  12.       /spring-mvc-3.0.xsd">
  13.       
  14.     
  15.     <bean id="User" class="cn.edu.shou.hsh.model.User"></bean>
  16.     <bean id="UserDAO" class="cn.edu.shou.hsh.Impl.UserImpl"></bean>
  17.     <bean name="/hsh/search" class="cn.edu.shou.hsh.control.HelloWorldController">
  18.         <property name="User">
  19.          <ref local="User"/>
  20.         </property>
  21.         <property name="UserDAO">
  22.          <ref local="UserDAO"/>
  23.         </property>
  24.     </bean>
  25.     
  26.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  27.         <property name="prefix" value="/"></property>
  28.         <property name="suffix" value=".jsp"></property>
  29.     </bean>
  30.  </beans>


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
  3.  "">
  4. <sqlMapConfig>
  5.     <properties resource="config/SqlMapConfigExample.properties" />
  6.     <settings cacheModelsEnabled="true" enhancementEnabled="true"
  7.         lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
  8.         maxTransactions="5" useStatementNamespaces="false" />
  9.     <transactionManager type="JDBC">
  10.         <dataSource type="SIMPLE">
  11.             <property name="JDBC.Driver" value="${dirver}" />
  12.             <property name="JDBC.ConnectionURL" value="${url}" />
  13.             <property name="JDBC.Username" value="${username}" />
  14.             <property name="JDBC.Password" value="${password}" />
  15.         </dataSource>
  16.     </transactionManager>

  17.     <sqlMap resource="cn/edu/shou/hsh/model/User.xml" />

  18. </sqlMapConfig>


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3.     xmlns=""
  4.     xmlns:xsi=""
  5.     xsi:schemaLocation="
  6.     /web-app_2_5.xsd">
  7.   <display-name></display-name>    
  8.   <welcome-file-list>
  9.     <welcome-file>index.jsp</welcome-file>
  10.   </welcome-file-list>
  11.   <servlet>
  12.       <servlet-name>springMVC</servlet-name>
  13.       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  14.           <init-param>
  15.               <param-name>contextConfigLocation</param-name>
  16.               <param-value>classpath*:config/spring-servlet.xml</param-value>
  17.           </init-param>
  18.       <load-on-startup>1</load-on-startup>
  19.   </servlet>
  20.   
  21.   <servlet-mapping>
  22.       <servlet-name>springMVC</servlet-name>
  23.       <url-pattern>/</url-pattern>
  24.   </servlet-mapping>
  25. </web-app>


点击(此处)折叠或打开

  1. dirver=com.microsoft.sqlserver.jdbc.SQLServerDriver
  2. url=jdbc:sqlserver://127.0.0.1;DatabaseName=User
  3. username=shou
  4. password=shou


点击(此处)折叠或打开

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib prefix="c" uri="" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     
  12.     <title>My JSP 'index.jsp' starting page</title>
  13.     <meta http-equiv="pragma" content="no-cache">
  14.     <meta http-equiv="cache-control" content="no-cache">
  15.     <meta http-equiv="expires" content="0">
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17.     <meta http-equiv="description" content="This is my page">
  18.     
  19.     <script type="text/javascript">
  20.         function Check(){
  21.         //alert(document.getElementById("username").value);
  22.             if(document.getElementById("username").value==""){
  23.                 document.getElementById("info").innerHTML="请输入用户名!";
  24.                 document.getElementById("info").style.color='red';
  25.                 document.getElementById("username").focus();
  26.                 return false;
  27.             }else if(document.getElementById("password").value=="") {
  28.                 document.getElementById("info").innerHTML="请输入密码!";
  29.                 document.getElementById("info").style.color='red';
  30.                 document.getElementById("password").focus();
  31.                 return false;
  32.             }
  33.         }
  34.     </script>
  35.   </head>
  36.   
  37.   <body>
  38.     <br/>
  39.     <form action="hsh/search" method="post" name="myForm" onsubmit="return Check();">
  40.     <div><div id="info"></div>
  41.         <table border=0>
  42.         <tr><td>用户名:</td><td align="center"><input type="text" name="username" id="username"></td></tr>
  43.         <tr><td>&nbsp;码:</td><td align="center"><input type="password" name="password" id="password"><br/></td></tr>
  44.         <tr ><td align="center" colspan="2"><input type="submit" value="提交"></td></tr>    
  45.         </table>
  46.     </div>
  47.     </form>
  48.   </body>
  49. </html>


点击(此处)折叠或打开

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib prefix="c" uri="" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     
  12.     <title>My JSP 'index.jsp' starting page</title>
  13.     <meta http-equiv="pragma" content="no-cache">
  14.     <meta http-equiv="cache-control" content="no-cache">
  15.     <meta http-equiv="expires" content="0">
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17.     <meta http-equiv="description" content="This is my page">
  18.     
  19.   </head>
  20.   
  21.   <body>
  22.     <br/>
  23.     <h1>查询结果:</h1>
  24.     <div style="color:red;">
  25.         <c:forEach items="${map }" var="m">
  26.         ${m.key } -----> ${m.value }</br>    
  27.         </c:forEach>
  28.     </div>
  29.   </body>
  30. </html>



阅读(3535) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~