asp.net mvc 理论群:516356157 人还少
index.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>
<jsp:forward page="/WEB-INF/login.jsp"></jsp:forward>
</body>
</html>
login.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>
<div align="center">
<h1>管理员登陆</h1>
<form action="/test/login.do?flag=login" method="post">
<table>
<tr><td>id:</td><td><input name="name" type="text" style="width: 158px; "></input></td></tr>
<tr><td>pw:</td><td><input name="pwd" type="password" style="width: 158px; "></input></td></tr>
<tr><td><td><input name="submit" type="submit" value="登 录"></input> <input type="reset" value="重 置"></input></td></tr>
</table>
</form>
</div>
</body>
</html>
mainFrame.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>
<h2>请选择你要的操作 </h2>
<a href="#">添加雇员</a>
<a href="#">显示雇员</a>
<a href="#">查询雇员</a>
<a href="#">退出系统</a>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.iflve.web.forms.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" parameter="flag" name="loginForm" type="com.iflve.web.action.LoginDispatchAction" >
<forward name="ok" path="/WEB-INF/mainFrame.jsp"></forward>
<forward name="err" path="/WEB-INF/login.jsp"></forward>
</action>
</action-mappings>
</struts-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts -->
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 在web容器中初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 配置数据源,在spring的spring-framework-reference中有详细介绍 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3"></property>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"></property>
<!-- 最大空闲值 -->
<property name="maxIdle" value="2"></property>
<!-- 最小空闲值 -->
<property name="minIdle" value="1"></property>
<!-- java链接数据库JDBC,Spring提倡使用数据源
①初始化时配置若干个数据库链接,放入连接池中【 连接池启动时的初始值initialSize】。
②当初始值的连接已满,则会创建新的连接,但是新创建的连接数量不会超过maxactive所设置的连接。超出最大值后放入队列池,等待前面的链接释放。
③空闲时最大空闲2个链接。
④最小空闲值:空闲时最少保持一个链接 不间断。
-->
</bean>
<!-- 配置SessionFactory[会话工厂] -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 设置数据源 -->
<property name="dataSource" ref="myDataSource"></property>
<!-- 接管Hibernate的对象映射文件集合 -->
<property name="mappingResources">
<list>
<value>com/iflve/domain/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
<!-- hibernate方言 -->
hibernate.dialect=org.hibernate.dialect.MySQLDialect
<!-- 自动根据对象和关系映射文件创建数据库 -->
hibernate.hbm2ddl.auto=update
<!-- 显示被翻译的sql语句 -->
hibernate.show_sql=false
</value>
</property>
</bean>
<!-- 配置EmployeeService -->
<bean id="employeeService" class="com.iflve.service.imp.EmployeeService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器接管employee [spring的事务管理器接管hibernate的事务处理 @Transactional] -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
Employee.java //
package com.iflve.domain;
import java.util.Date;
public class Employee {
private Integer id;
private String name;
private String pwd;
private java.util.Date hiredate;
private float salary;
private String email;
private String grade;
//无参数的构造函数去掉会报错
public Employee(){
}
//该构造函数用于在hibernate的数据库关系映射文件中创建表
public Employee(String name, String pwd, String grade, Date hiredate, float salary,
String email) {
this.name = name;
this.pwd = pwd;
this.grade = grade;
this.hiredate = hiredate;
this.salary = salary;
this.email = email;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public java.util.Date getHiredate() {
return hiredate;
}
public void setHiredate(java.util.Date hiredate) {
this.hiredate = hiredate;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
//hiberante关系映射文件用于创建数据表,其内容对应employee中的带参数的构造函数.
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iflve.domain">
<class name="Employee" table="employee">
<!-- 主键生成策略 -->
<id name="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<!-- 属性名字 -->
<property name="name" type="java.lang.String">
<column name="name" length="64"></column>
</property>
<!-- 属性入职时间密码 -->
<property name="pwd" type="java.lang.String">
<column name="pwd" length="32"></column>
</property>
<!-- 属性级别-->
<property name="grade" type="java.lang.String">
<column name="grade" length="16"></column>
</property>
<!-- 属性入职时间 -->
<property name="hiredate" type="java.util.Date">
<column name="hiredate"></column>
</property>
<!-- 属性薪水 -->
<property name="salary" type="java.lang.Float">
<column name="salary"></column>
</property>
<!-- 属性email -->
<property name="email" type="java.lang.String">
<column name="email" length="64"></column>
</property>
</class>
</hibernate-mapping>
//目标对象的Service 该类实现了EmployeeServiceInter接口
EmployeeService.java
package com.iflve.service.imp;
import java.io.Serializable;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import com.iflve.domain.Employee;
import com.iflve.service.inter.EmployeeServiceInter;
//这里配置这个注解,是让spring的事务管理器接管该service的所有事务
@Transactional//beans.xml中声明的事务管理器,由spring来接管hibernate的事务处理
public class EmployeeService implements EmployeeServiceInter {
private SessionFactory sessionFactory; //生成get和set方法,对应beans.mxl中的一个bean
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void add(Employee emp) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(emp);
}
@Override
public List<Employee> showEmployee() {
// TODO Auto-generated method stub
return null;
}
@Override
public void update(Employee e) {
// TODO Auto-generated method stub
}
@Override
public void delEmployee(Serializable id) {
// TODO Auto-generated method stub
}
@Override
public Employee getEmployeeByID(Serializable id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Employee checkEmployee(Employee emp) {
// TODO Auto-generated method stub]
//String sql = "select *from employee where name=? and pwd=?"
//sessionFactory.getCurrentSession().createSQLQuery(sql).setString(0, arg1)
return null;
}
}
//servive的接口
EmployeeServiceInter.java
/**
*
*/
package com.iflve.service.inter;
import java.util.List;
import com.iflve.domain.Employee;
/**
* @author sjcx
*
*/
public interface EmployeeServiceInter {
public void add(Employee emp);
public List<Employee> showEmployee();
public void update(Employee e);
public void delEmployee(java.io.Serializable id);
//通过id获取Employee
public Employee getEmployeeByID(java.io.Serializable id);
//检查用户是否存在 如果存在则返货完整id 如果不存在则返回null
public Employee checkEmployee(Employee emp);
}
//登录验证的action
LoginDispatchAction.java
package com.iflve.web.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iflve.service.inter.EmployeeServiceInter;
import com.iflve.web.forms.LoginForm;
public class LoginDispatchAction extends DispatchAction {
private WebApplicationContext webApplicationContext;
public ActionForward login(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
System.out.println("11111111111111111111111111");
LoginForm loginForm=(LoginForm)arg1;
if(loginForm.getPwd().equals("123")){
return arg0.findForward("ok");
}else{
return arg0.findForward("err");
}
}
public ActionForward loginout(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
return super.execute(arg0, arg1, arg2, arg3);
}
}
//该类中的属性对应于提交表单中要提交的字段,为其生成get和set方法
LoginForm.java
package com.iflve.web.forms;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
private String pwd;
}
需要用到的包: 在网盘中
重点关注:
①Hibernate的关系映射文件Hibernate.hbm.xml如何映射类中的构造函数。
②spring接管sessionFactory的事务管理 [beans.xml中声明事务管理器,在事务处理的地方@Transactional]
③在tomcat中初始化spring容器
④表单提交中报资源无效的问题