我的任务必须将数据保存在MS SQL服务器中。这就像简单的注册表格。我必须输入姓名,姓氏,地址和生日,然后按提交按钮。之后,输入数据应保存在数据库中。我不知道如何制作它。这是我的代码:
JSP PAGE:
<%@page contentType="text/html" 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>Welcome to Test Project</title>
</head>
<body>
<a href="customerList.htm">Customer List</a>
<h:form>
<h1>Add person</h1>
<p>Please fill in all fields and click Submit</p>
<label id="firstNameLabel">First name</label>
<input id="inputFirstName" value="${customerBean.firstName}"/>
<label id="lastNameLabel">Last name</label>
<input id="inputLastName" value="${customerBean.lastName}"/>
<label id="nirthdayLabel">Birthday name</label>
<input id="inputBirthday" value="${customerBean.birthday}"/>
<label id="addressLabel">Address name</label>
<input id="inputAddress" value="${customerBean.address}"/>
<button id="addAddPerson" value="#{customerBean., hsr1)">Add new person</button>
</h:panelGrid>
</h:form>
</body>
</html>
另一个JSP页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer List</title>
</head>
<body>
<h1>Customer List</h1>
<table border="1">
<th>First Name</th><th>Last Name</th><th>Birthday</th><th>Address</th>
<c:forEach items="${customerList}" var="customer">
<tr>
<td>${customer.firstName}</td><td>${customer.lastName}</td>
<td>${customer.birthday}</td><td>${customer.address}</td>
</tr>
</c:forEach>
</table>
<a href="addPerson.htm">Add new person</a>
<a href="index.htm">Home</a>
</body>
</html>
Spring bean xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
更多XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="customerController"
class="Controller.CustomerController"/>
</beans>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
Hibernate配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost\TARAS-PC\SQLEXPRESS:1433;databaseName=Test</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">test</property>
<!--Add by myself-->
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<mapping resource="Model/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Java控制器:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
import Model.Customer;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
*
* @author Taras
*/
public class CustomerController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
ModelAndView mv = new ModelAndView("customerList");
List<Customer> customerList;
try{
//import from package Util
Session session = Util.HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
customerList = session.createQuery("from Customer").list();
// customerList.add(new Customer(new Customer().getFirstName(), new Customer().getLastName(),
// new Customer().getAddress(), new Customer().getBirthday()));
mv.addObject("customerList", customerList);
session.getTransaction().commit();
}
catch (Exception e){
e.printStackTrace();
}
//customerList.add(new Customer("ds", "sf", "sfs", "sf"));
return mv;
}
}
生成的java类:
package Model;
// Generated Mar 4, 2014 11:21:14 PM by Hibernate Tools 3.2.1.GA
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Test generated by hbm2java
*/
@Entity
@ManagedBean (name = "customerBean")
@Table(name="Test"
,schema="dbo"
,catalog="Test"
)
public class Customer implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
private String address;
private String birthday;
public Customer() {
}
public Customer(String firstName, String lastName, String address, String birthday) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.birthday = birthday;
}
@Id
@Column(name="ID", unique=true, nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="FirstName")
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LastName")
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name="Address")
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="Birthday")
public String getBirthday() {
return this.birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
// public String addEntry(){
// if (firstName != null && lastName != null && birthday != null && address != null)
// return "";
// else
// return "";
// }
}