准备工作:struts2.3.34+spring4.2.2+hibernate3.3.2
导入需要的开发包:
struts开发包---注意:javassist-3.18.1-GA.jar包与hibernate中的重复(只保留高版本即可)
hibernate开发包
此外还需要在hibernate解压包中找到lib/optional/c3p0文件夹,然后将c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar拷贝到工程lib目录下
spring开发包
除了javadoc.jar和sources.jar包名结尾的包,其他包全部拷贝到工程项目lib中
最后别忘了将mysql驱动包拷贝到lib中
2、web.xml
Spring提供了ContextLoaderListener,该监听器实现了ServletContextListener接口,他在Web应用程序启动时被触发。当他创建时会自动查找WEB-INF/下的applicationContext.xml,所以当只有一个配置文件且文件名为applicationContext.xml时,则只需要在web.xml文件中配置ContextLoaderListener监听器即可.当有多个配置文件需要载入,则应该使用<context-param>元素指定配置文件的文件名,ContextLoaderListener加载时,会查找名为contextConfigLocation的初始化参数。当Web应用程序启动时先读取web.xml文件,然后创建spring容器,之后根据配置文件内容,装配Bean实例。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>sklm_2</display-name> 4 <welcome-file-list> 5 <!-- 6 <welcome-file>index.html</welcome-file> 7 <welcome-file>index.htm</welcome-file> 8 <welcome-file>index.jsp</welcome-file> 9 <welcome-file>default.html</welcome-file> 10 <welcome-file>default.htm</welcome-file> 11 --> 12 <welcome-file>home.jsp</welcome-file> 13 </welcome-file-list> 14 15 <!-- 让spring随web启动而创建的监听器 --> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 <!-- 配置spring配置文件位置参数 --> 20 <context-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:applicationContext.xml</param-value> 23 </context-param> 24 25 <!-- struts2核心配置 --> 26 <filter> 27 <filter-name>struts2</filter-name> 28 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 29 </filter> 30 <filter-mapping> 31 <filter-name>struts2</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 35 </web-app>
3、实体和映射文件
(1)User.java
1 package cn.xyp.web.entity; 2 3 import java.sql.Date; 4 5 public class User { 6 7 private Long user_id; 8 private String user_name; 9 private String user_password; 10 private int user_age; 11 private String user_sex; 12 private String user_address; 13 private Date user_create_time; 14 public User(){} 15 16 public User(String user_name, String user_password, int user_age, String user_sex, String user_address, 17 Date user_create_time) { 18 this.user_name = user_name; 19 this.user_password = user_password; 20 this.user_age = user_age; 21 this.user_sex = user_sex; 22 this.user_address = user_address; 23 this.user_create_time = user_create_time; 24 } 25 26 public Long getUser_id() { 27 return user_id; 28 } 29 public void setUser_id(Long user_id) { 30 this.user_id = user_id; 31 } 32 public String getUser_name() { 33 return user_name; 34 } 35 public void setUser_name(String user_name) { 36 this.user_name = user_name; 37 } 38 public String getUser_password() { 39 return user_password; 40 } 41 public void setUser_password(String user_password) { 42 this.user_password = user_password; 43 } 44 public int getUser_age() { 45 return user_age; 46 } 47 public void setUser_age(int user_age) { 48 this.user_age = user_age; 49 } 50 public String getUser_sex() { 51 return user_sex; 52 } 53 public void setUser_sex(String user_sex) { 54 this.user_sex = user_sex; 55 } 56 public String getUser_address() { 57 return user_address; 58 } 59 public void setUser_address(String user_address) { 60 this.user_address = user_address; 61 } 62 public Date getUser_create_time() { 63 return user_create_time; 64 } 65 public void setUser_create_time(Date user_create_time) { 66 this.user_create_time = user_create_time; 67 } 68 69 @Override 70 public String toString(){ 71 return "User [user_id="+user_id+", user_name="+user_name+", user_password="+user_password+", user_age="+user_age+", " 72 + "user_sex="+user_sex+", user_address="+user_address+", user_create_time="+user_create_time+"]"; 73 } 74 75 }
(2)、User.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.xyp.web.entity"> 7 <class name="User" table="sys_user"> 8 <id name="user_id" type="long"> 9 <generator class="native"></generator> 10 </id> 11 12 <property name="user_name" type="java.lang.String"></property> 13 <property name="user_password" type="java.lang.String"></property> 14 <property name="user_age" type="int"></property> 15 <property name="user_sex" type="java.lang.String"></property> 16 <property name="user_address" type="java.lang.String"></property> 17 <property name="user_create_time" type="java.sql.Date"></property> 18 </class> 19 </hibernate-mapping>
4、DAO
(1)UserDAO.java接口类
1 package cn.xyp.web.dao; 2 3 import java.util.List; 4 5 import cn.xyp.web.entity.User; 6 7 public interface UserDAO { 8 9 /** 10 * 向数据库中添加用户 11 * @param user 12 */ 13 public void add(User user); 14 15 /** 16 * 刷新user在数据库中的信息 17 * @param user 18 */ 19 public void update(User user); 20 21 /** 22 * 根据被给定的id号删除对应的user 23 * @param id 24 */ 25 public void delete(long id); 26 27 /** 28 * 根据被给定的name和password查找user 29 * @param name 30 * @param password 31 * @return 32 */ 33 public User findByName(String name, String password); 34 35 /** 36 * 根据被给定的id号查找user 37 * @param id 38 * @return 39 */ 40 public User findById(long id); 41 42 /** 43 * 从数据库中查找所有信息 44 * @return 45 */ 46 public List<User> findAll(); 47 }
(2)UserDAOImpl.java 接口实现类
1 package cn.xyp.web.dao; 2 3 import java.util.List; 4 5 import org.hibernate.HibernateException; 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction; 10 11 import cn.xyp.web.entity.User; 12 13 public class UserDAOImpl implements UserDAO { 14 15 private SessionFactory sessionFactory; 16 17 //构造方法注入SessionFactory对象 18 public UserDAOImpl(SessionFactory sessionFactory){ 19 this.sessionFactory = sessionFactory; 20 } 21 22 //使用SessionFactory对象返回Session对象 23 public Session currentSession(){ 24 return sessionFactory.openSession(); 25 } 26 27 @Override 28 public void add(User user) { 29 Session session = null; 30 try { 31 session = currentSession(); 32 Transaction tx = session.beginTransaction(); 33 session.save(user); 34 tx.commit(); 35 } catch (HibernateException e) { 36 e.printStackTrace(); 37 }finally{ 38 session.close(); 39 } 40 41 } 42 43 @Override 44 public void update(User user) { 45 Session session = null; 46 try { 47 session = currentSession(); 48 Transaction tx = session.beginTransaction(); 49 session.save(user); 50 tx.commit(); 51 } catch (HibernateException e) { 52 e.printStackTrace(); 53 }finally{ 54 session.close(); 55 } 56 57 } 58 59 @Override 60 public void delete(long id) { 61 Session session = null; 62 try { 63 session = currentSession(); 64 Transaction tx = session.beginTransaction(); 65 User user = (User)session.get(User.class, id); 66 session.delete(user); 67 tx.commit(); 68 } catch (HibernateException e) { 69 e.printStackTrace(); 70 }finally{ 71 session.close(); 72 } 73 74 } 75 76 @Override 77 public User findByName(String name, String password) { 78 Session session = null; 79 User user = null; 80 try { 81 session = currentSession(); 82 Transaction tx = session.beginTransaction(); 83 String hsql = "from User m where m.user_name = :uname and m.user_password= :upassword"; 84 Query query = session.createQuery(hsql); 85 query.setParameter("uname", name); 86 query.setParameter("upassword", password); 87 user = (User) query.uniqueResult(); 88 tx.commit(); 89 }catch(HibernateException e){ 90 e.printStackTrace(); 91 }finally{ 92 session.close(); 93 } 94 95 return user; 96 } 97 98 @Override 99 public User findById(long id) { 100 Session session = null; 101 User user = null; 102 try { 103 session = currentSession(); 104 Transaction tx = session.beginTransaction(); 105 String hsql = "from User u where u.user_id= :id"; 106 Query query = session.createQuery(hsql); 107 user = (User) query.setParameter("id", id); 108 tx.commit(); 109 } catch (HibernateException e) { 110 e.printStackTrace(); 111 }finally{ 112 session.close(); 113 } 114 return user; 115 } 116 117 @Override 118 public List<User> findAll() { 119 Session session = null; 120 List<User> list = null; 121 try { 122 session = currentSession(); 123 Transaction tx = session.beginTransaction(); 124 String hsql = "from User"; 125 Query query = session.createQuery(hsql); 126 list = query.list(); 127 tx.commit(); 128 } catch (HibernateException e) { 129 e.printStackTrace(); 130 }finally{ 131 session.close(); 132 } 133 return list; 134 } 135 136 }
5、业务逻辑层
(1)UserService.java接口类
1 package cn.xyp.web.service; 2 3 import java.util.List; 4 5 import cn.xyp.web.entity.User; 6 7 public interface UserService { 8 9 public void add(User user); 10 public void update(User user); 11 public void delete(long id); 12 public User findByName(String name, String password); 13 public User findById(long id); 14 public List<User> findAll(); 15 16 }
(2)UserServiceImpl.java接口实现类
1 package cn.xyp.web.service; 2 /** 3 * 设值注入DAO层组件 4 */ 5 import java.util.List; 6 7 import cn.xyp.web.dao.UserDAO; 8 import cn.xyp.web.entity.User; 9 10 public class UserServiceImpl implements UserService { 11 12 private UserDAO userDao; 13 14 public void setUserDao(UserDAO userDao){ 15 this.userDao = userDao; 16 } 17 18 @Override 19 public void add(User user) { 20 21 if(userDao.findById(user.getUser_id())==null){ 22 userDao.add(user); 23 } 24 } 25 26 @Override 27 public void update(User user) { 28 if(userDao.findById(user.getUser_id())==null){ 29 userDao.update(user); 30 } 31 32 } 33 34 @Override 35 public void delete(long id) { 36 if(userDao.findById(id)==null){ 37 userDao.delete(id); 38 } 39 40 } 41 42 @Override 43 public User findByName(String name, String password) { 44 User user = userDao.findByName(name, password); 45 return user; 46 } 47 48 @Override 49 public User findById(long id) { 50 User user = userDao.findById(id); 51 return user; 52 } 53 54 @Override 55 public List<User> findAll() { 56 List<User> list = userDao.findAll(); 57 return list; 58 } 59 60 }
6、Action
(1)UserRegisterAction.java类(用户注册类)
1 package cn.xyp.web.action; 2 /** 3 *设置注入业务逻辑组件 4 */ 5 import com.opensymphony.xwork2.ActionSupport; 6 7 import cn.xyp.web.entity.User; 8 import cn.xyp.web.service.UserService; 9 10 public class UserRegisterAction extends ActionSupport { 11 12 /** 13 * 14 */ 15 private static final long serialVersionUID = -4501870315633237256L; 16 private User user; 17 private UserService userService; 18 public User getUser(){ 19 return user; 20 } 21 public void setUser(User user){ 22 this.user = user; 23 } 24 25 //注入业务逻辑组件 26 public void setUserService(UserService userService){ 27 this.userService = userService; 28 } 29 30 public String execute(){ 31 userService.add(user); 32 return SUCCESS; 33 } 34 }
(2)UserLoginAction.java类(用户登录类)
1 package cn.xyp.web.action; 2 /** 3 * 设置用户登录注入 4 */ 5 import com.opensymphony.xwork2.ActionSupport; 6 7 import cn.xyp.web.entity.User; 8 import cn.xyp.web.service.UserService; 9 10 public class UserLoginAction extends ActionSupport { 11 12 /** 13 * 14 */ 15 private static final long serialVersionUID = 7448295814371243220L; 16 17 private User user; 18 private UserService userService; 19 public User getUser() { 20 return user; 21 } 22 public void setUser(User user) { 23 this.user = user; 24 } 25 //注入业务逻辑组件 26 public void setUserService(UserService userService) { 27 this.userService = userService; 28 } 29 30 public String execute(){ 31 String b = null; 32 try { 33 User user_ = userService.findByName(user.getUser_name(), user.getUser_password()); 34 if(user_ != null){ 35 b = SUCCESS; 36 } 37 } catch (Exception e) { 38 b = ERROR; 39 } 40 return b; 41 } 42 43 }
(3)UserDeleteAction.java类(删除用户类)
1 package cn.xyp.web.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 import cn.xyp.web.service.UserService; 6 7 public class UserDeleteAction extends ActionSupport { 8 9 /** 10 * 删除用户 11 */ 12 private static final long serialVersionUID = -2132782345900294714L; 13 private UserService userService; 14 private long id; 15 16 public void setUserService(UserService userService) { 17 this.userService = userService; 18 } 19 public long getId() { 20 return id; 21 } 22 public void setId(long id) { 23 this.id = id; 24 } 25 26 public String execute(){ 27 userService.delete(getId()); 28 return SUCCESS; 29 } 30 31 }
(4)UserQueryAction.java(查询所有用户)
1 package cn.xyp.web.action; 2 /** 3 * 查找数据库中的所有信息 4 */ 5 import java.util.List; 6 import javax.servlet.http.HttpServletRequest; 7 import org.apache.struts2.ServletActionContext; 8 import com.opensymphony.xwork2.ActionSupport; 9 import cn.xyp.web.entity.User; 10 import cn.xyp.web.service.UserService; 11 12 public class UserQueryAction extends ActionSupport { 13 14 /** 15 * 16 */ 17 private static final long serialVersionUID = 1483788569708897545L; 18 private UserService userService; 19 20 public void setUserService(UserService userService) { 21 this.userService = userService; 22 } 23 24 public String execute(){ 25 List<User> list = userService.findAll(); 26 HttpServletRequest request = ServletActionContext.getRequest(); 27 request.setAttribute("userList", list); 28 return SUCCESS; 29 } 30 31 }
(5)UserUpdateAction.java(更新用户)
1 package cn.xyp.web.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 import cn.xyp.web.entity.User; 6 import cn.xyp.web.service.UserService; 7 8 public class UserUpdateAction extends ActionSupport { 9 10 /** 11 * 更新用户信息 12 */ 13 private static final long serialVersionUID = 6174197960048716176L; 14 15 private UserService userService; 16 private User user; 17 private long id; 18 public UserService getUserService() { 19 return userService; 20 } 21 public void setUserService(UserService userService) { 22 this.userService = userService; 23 } 24 public User getUser() { 25 return user; 26 } 27 public void setUser(User user) { 28 this.user = user; 29 } 30 public long getId() { 31 return id; 32 } 33 public void setId(long id) { 34 this.id = id; 35 } 36 37 public String showUser(){ 38 User user = userService.findById(id); 39 setUser(user); 40 return SUCCESS; 41 } 42 43 public String execute(){ 44 userService.update(user); 45 return SUCCESS; 46 } 47 48 }
7、struts.xml
spring托管将Struts2中的Action的实例化工作交由Spring容器统一管理,同时使Struts2中的Action实例能够访问Spring提供的业务逻辑资源,spring容器依赖注入的优势也可以体现,Struts2提供的spring插件struts2-spring-plugin-2.3.16.3.jar配合<constant name="struts.objectFactory" value="spring">使得Struts2的action由Spring来负责进行实例化.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 7 <!-- spring配置 --> 8 <!-- 告诉struts运行时使用spring来创建对象 --> 9 <constant name="struts.objectFactory" value="spring"/> 10 11 <package name="crm" extends="struts-default"> 12 <!-- class属性值是spring定义的Bean,当执行execute()返回success时,控制转到另一个动作userQuery --> 13 <action name="userRegister" class="userRegisterAction"> 14 <result name="success" type="redirectAction">/userQuery</result> 15 </action> 16 17 <action name="userLogin" class="userLoginAction"> 18 <result name="success">/welcome.jsp</result> 19 <result name="error">/register.jsp</result> 20 </action> 21 22 <action name="userQuery" class="userQueryAction"> 23 <result name="success">/displayAll.jsp</result> 24 </action> 25 26 <action name="userDelete" class="userDeleteAction"> 27 <result name="success" type="redirectAction">/userQuery</result> 28 </action> 29 30 <action name="userShow" class="userShowAction" method="showUser"> 31 <result name="success">/update.jsp</result> 32 </action> 33 34 <action name="userUpdate" class="userUpdateAction"> 35 <result name="success" type="redirectAction">/userQuery</result> 36 </action> 37 38 </package> 39 </struts>
8、applicationContext.xml
spring 框架提供了多种数据源类,可以使用spring提供的DriverManagerDataSource类还可以使用第三方数据源,如C3P0的ComboPooledDataSource数据源类,注意相应的两个jar包c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar.
Hibernate4已经完全实现自己的事物管理,所以spring4不提供HibernateDaoSupport和HibernateTemplete的支持。可以在应用程序的spring上下文中,像配置其他bean那样来配置HibernateSession工厂。如果要使用XML文件定义对象与数据库之间的映射,则需要在spring中配置LocalSessionFactoryBean。hibernateProperties属性配置了Hibernate如何进行操作的细节。"hibernate.current_session_context_class"是为当前Session提供一个策略,Session由org.springframework.orm.hibernate4.SpringSessionContext.currentSession得到。将sessionFactory注入到其他Bean中,如注入到DAO组件中,使其获得SessionFactory的引用后,就可以实现对数据库的访问。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jee="http://www.springframework.org/schema/jee" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 13 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 15 16 17 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 18 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 19 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sklm?useUnicode=true&characterEncoding=UTF-8"></property> 20 <property name="user" value="root"></property> 21 <property name="password" value="123456"></property> 22 <property name="maxPoolSize" value="40"></property> 23 <property name="minPoolSize" value="1"></property> 24 <property name="initialPoolSize" value="2"></property> 25 <property name="maxIdleTime" value="20"></property> 26 27 </bean> 28 29 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource"></property> 31 <property name="mappingResources"> 32 <list> 33 <value>cn/xyp/web/entity/User.hbm.xml</value> 34 </list> 35 </property> 36 <property name="hibernateProperties"> 37 <props> 38 <prop key="hibernate.show_sql">true</prop> 39 <prop key="hibernate.hbm2ddl.auto">true</prop> 40 <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 41 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop> 42 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 43 </props> 44 </property> 45 </bean> 46 47 48 49 <bean id="userDao" class="cn.xyp.web.dao.UserDAOImpl"> 50 <!-- 构造方法注入会话工厂组件sessionFactory --> 51 <constructor-arg> 52 <ref bean="sessionFactory"/> 53 </constructor-arg> 54 </bean> 55 56 <bean id="userService" class="cn.xyp.web.service.UserServiceImpl"> 57 <!-- 设置注入DAO组件 --> 58 <property name="userDao" ref="userDao"></property> 59 </bean> 60 61 <bean id="userRegisterAction" class="cn.xyp.web.action.UserRegisterAction"> 62 <!-- 设置业务逻辑组件 --> 63 <property name="userService" ref="userService"></property> 64 </bean> 65 66 <bean id="userLoginAction" class="cn.xyp.web.action.UserLoginAction"> 67 <property name="userService" ref="userService"></property> 68 </bean> 69 70 <bean id="userQueryAction" class="cn.xyp.web.action.UserQueryAction"> 71 <property name="userService" ref="userService"></property> 72 </bean> 73 74 <bean id="userDeleteAction" class="cn.xyp.web.action.UserDeleteAction"> 75 <property name="userService" ref="userService"></property> 76 </bean> 77 78 <bean id="userUpdateAction" class="cn.xyp.web.action.UserUpdateAction"> 79 <property name="userService" ref="userService"></property> 80 </bean> 81 82 <bean id="homeAction" class="cn.xyp.web.action.HomeAction"> 83 84 </bean> 85 86 </beans>
9、jsp
(1)displayAll.jsp显示所有信息
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>显示会员信息</title> 9 </head> 10 <body> 11 12 <h4>会员信息</h4> 13 <table border="1"> 14 <tr> 15 <td>会员id</td><td>会员名</td><td>密码</td><td>会员年龄</td><td>会员性别</td><td>会员联系方式</td><td>会员地址</td><td>注册会员日期</td><td>删除</td><td>修改</td> 16 </tr> 17 <s:iterator value="#request.userList" id="user"> 18 <tr> 19 <td><s:property value="#user.user_id"/></td> 20 <td><s:property value="#user.user_name"/></td> 21 <td><s:property value="#user.user_password"/></td> 22 <td><s:property value="#user.user_age"/></td> 23 <td><s:property value="#user.user_sex"/></td> 24 <td><s:property value="#user.user_phone"/></td> 25 <td><s:property value="#user.user_address"/></td> 26 <td><s:property value="#user.user_create_time"/></td> 27 28 <td> 29 <a href="<s:url action="userDelete"><s:param name="id"><s:property value="#user.user_id"/></s:param></s:url>">删除</a> 30 </td> 31 <td> 32 <a href="<s:url action="userShow"><s:param name="id"><s:property value="#user.user_id"/></s:param></s:url>">修改</a> 33 </td> 34 35 </tr> 36 </s:iterator> 37 </table> 38 <a href="register.jsp">返回注册页面</a> 39 40 </body> 41 </html>
(2) login.jsp用户登录
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>会员登陆</title> 9 </head> 10 <body> 11 12 <s:form action="userLogin" method="post"> 13 <s:textfield name="user.user_name" label="会员姓名:"></s:textfield> 14 <s:password name="user.user_password" label="会员口令:"></s:password> 15 <s:submit value="登 陆"></s:submit> 16 </s:form> 17 18 </body> 19 </html>
(3)register.jsp用户注册
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>会员注册页面</title> 9 </head> 10 <body> 11 12 <s:form action="userRegister" method="post"> 13 <h4><s:text name="欢迎注册会员"></s:text></h4> 14 <s:property value="exception.message"/> 15 <s:textfield name="user.user_name" label="会员姓名 " tooltip="Enter your name!" required="true"></s:textfield> 16 <s:password name="user.user_password" label="会员口令 " tooltip="Enter your password!" required="true"></s:password> 17 <s:textfield name="user.user_age" label="年 龄 " required="true"></s:textfield> 18 <%-- <s:select name="user.user_sex" label="性 别" list="{'男','女'}" headKey="00" headValue="男" theme="simple" required="true"></s:select> --%> 19 <s:radio name="user.user_sex" label="性别" list="{'男','女'}" value="男"></s:radio> 20 <s:textfield name="user.user_address" label="会员住址 " required="true"></s:textfield> 21 <s:submit value="提 交"></s:submit> 22 </s:form> 23 24 </body> 25 </html>
(4)update.jsp用户信息更改
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>修改会员信息</title> 9 </head> 10 <body> 11 12 <s:form action="userUpdate" method="post"> 13 <h4><s:text name="修改会员信息"></s:text></h4> 14 <s:actionerror/> 15 16 <s:hidden name="user.user_id" value="%{user.user_id}"></s:hidden> 17 <s:textfield name="user.user_name" label="会员姓名:" tooltip="Enter your name!" required="true"></s:textfield> 18 <s:password name="user.user_password" label="会员口令:" tooltip="Enter your password!" required="true"></s:password> 19 <s:textfield name="user.user_age" label="年 龄:" required="true"></s:textfield> 20 <s:select name="user.user_sex" label="性 别:" list="{'男','女'}" headKey="00" headValue="男" theme="simple" required="true"></s:select> 21 <s:textfield name="user.user_address" label="会员住址:" required="true"></s:textfield> 22 <s:submit value="提 交"></s:submit> 23 </s:form> 24 25 </body> 26 </html>
(5)welcome.jsp欢迎页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>欢迎来到时空联盟</title> 9 <style type="text/css"> 10 body{ 11 background-image: url('WebContent/WEB-INF/img/shikong.png'); 12 background-position: center top; 13 background-repeat: no-repeat; 14 15 } 16 </style> 17 </head> 18 <body> 19 <h1>Hello World!</h1> 20 </body> 21 </html>
本文参考自https://www.cnblogs.com/zhaozihan/p/5893785.html