標籤:char str null welcome imp nbsp add 技術 目的
一:配置hibernate
1.導包 hibernate包和jdbc串連mysql資料庫的包
2.工具 + 生產力產生hibernate設定檔和對應檔
3.做好hibernateUtil產生session的靜態單例設定檔
二:編寫資料訪問
package com.dao;import java.util.List;import org.hibernate.Session;import com.model.HibernateUtil;import com.model.Login;public class LoginDao { Session session = null; public LoginDao(){ session =HibernateUtil.getSession(); } public List<Login> select(String name){ List<Login> list = null; try{ list = session.createQuery("from Login where UserName = ?") .setParameter(0, name) .getResultList(); } catch(Exception e){ e.getStackTrace(); } finally{ HibernateUtil.closeSession(); } return list; } public void add(Login l){ try{ session.beginTransaction(); session.save(l); session.getTransaction().commit(); } catch(Exception e){ e.getStackTrace(); session.getTransaction().rollback(); } finally{ HibernateUtil.closeSession(); } }}
三:配置struts
在apps檔案夾下將struts-blank.war複製粘貼將尾碼改為rar格式,解壓到這個目錄下,這樣就產生了一個空白的項目,這裡邊的WEB-INF檔案下lib裡邊的jar包就是我們struts所需要必須的jar包,另外如果需要用jstl標籤,就把jstl的jar包也匯入我們項目的WEB-INF檔案下的lib中
2.配置web.xml()
這裡的web.xml一樣是剛剛產生的空白項目中的WEB-INF檔案下的空白web.xml檔案,複製粘貼到項目中,將我們不需要用的刪除即可,只留下我們需要的內容如下:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
四、做介面
reg.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <form action="reg" method="post"> 帳號:<input type="text" name="username" ><br> 暱稱:<input type="text" name="name"><br> 密碼:<input type="password" name="password" ><br> 金額:<input type="text" name="account"> <input type="submit" value="提交"> </form> <form action="ttt"> 123123 <input type="submit" value="提交"> </form></body></html>
index_add.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <form action="Index_insert" method="post"> 帳號:<input type="text" name="username" ><br> 暱稱:<input type="text" name="name"><br> 密碼:<input type="password" name="password" ><br> 金額:<input type="text" name="account"> <input type="submit" value="提交"> </form></body></html>
配置IndexAction.java檔案
package com.controller;import java.math.BigDecimal;import java.util.List;import com.dao.LoginDao;import com.model.Login;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport { private String username; private String name; private String password; private BigDecimal account; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public BigDecimal getAccount() { return account; } public void setAccount(BigDecimal account) { this.account = account; } public String add(){ return SUCCESS; } public String insert(){ return SUCCESS; } @Override public String execute() throws Exception { List<Login> list=new LoginDao().select(username); if(list.size()==0){ Login l = new Login(); l.setUserName(username); l.setName(name); l.setPassword(password); l.setAccount(account); new LoginDao().add(l); return SUCCESS; } else{ return ERROR; } }}
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="reg" class="com.controller.IndexAction">//直接在地址欄運行reg.jsp,然後跳轉reg到IndexAction下的execute方法,成功走reg_ok.jsp <result name="success"> reg_ok.jsp </result> <result name="error"> reg_error.jsp </result> </action> <!-- <action name="Index_add" class="com.controller.IndexAction" method="add">//直接在IndexAction中寫入add與insert方法,地址欄直接輸入Index_add跳轉到add介面
在Index_add.jsp中提交到Index_insert,然後走下面的方法來實現功能 <result> Index_add.jsp </result> </action> <action name="Index_insert" class="com.controller.IndexAction" method="insert"> <result> Index_insert.jsp </result> </action> <action name="*_*" class="com.controller.{1}Action" method="{2}">//這個方法是最簡單的,前面的*代表上文所寫的Index,後面的代表在IndexAction中縮所寫的跳轉的方法
可以免去上文那麼麻煩的寫法,唯寫一個action即可解決所有問題, <result> {1}_{2}.jsp </result> </action> --> </package> </struts>
運行起來輸入action中的*_*的地址即可顯示出來。
利用hibernate與struts架構製作簡單註冊介面