一个简单的注册框架
myEclipse 新建一个web project
MySQL新建一个 user表
首先建立首页面 regsuccess.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>注册成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<div align="center" style="color: red;font-size: 100px;">
${user.username }注册成功!
</div>
</body>
</html>
然后是成功页面success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<div align="center" style="color: red;font-size: 100px;">
${user.username }登录成功!
</div>
</body>
</html>
接下来是跳转xml文件 xitongguanli.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>
<!-- 配置了这个属性之后修改struts.xml文件后就不需要重启 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="xitongguanli" namespace="/" extends="base">
<action name="Login" class="LoginAction">
<result name="success">xitongguanli/success.jsp</result>
<result name="error">xitongguanli/error.jsp</result>
</action>
<action name="Register" class="RegisterAction">
<result name="success">xitongguanli/regsuccess.jsp</result>
</action>
</package>
</struts>
就下来以此是RegisterAction.java,IUserDao.java,User.java,IUserService.java,user.xml,xitongguanli.xml,jdbc.properties
1. RegisterAction.java
package com.yy.xitongguanli.action;
import com.opensymphony.xwork2.ActionSupport;
import com.yy.xitongguanli.po.User;
import com.yy.xitongguanli.service.IUserService;
public class RegisterAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private IUserService userService;
public void setUserService(IUserService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
User user = new User();
user.setUsername(username);
user.setPassword(password);
userService.saveUser(user);
return SUCCESS;
}
}
2.IUserDao.java
package com.yy.xitongguanli.dao;
import java.util.Map;
import com.yy.xitongguanli.po.User;
public interface IUserDao {
public User getUserByMap(Map<String, Object> map);
public int saveUser(User user);
}
3.User.java
package com.yy.xitongguanli.po;
public class User {
private int userId;
private String username;
private int sex;
private String password;
private int flag;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
4.IUserService.java
package com.yy.xitongguanli.service;
import java.util.Map;
import com.yy.xitongguanli.po.User;
public interface IUserService {
public User getUserByMap(Map<String, Object> map);
public int saveUser(User user);
}
5.user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yy.xitongguanli.dao.IUserDao">
<select id="getUserByMap" resultType="com.yy.xitongguanli.po.User">
SELECT * FROM user where 1 = 1
<if test="username != null ">
and username = #{username}
</if>
<if test="password != null ">
and password = #{password}
</if>
</select>
<insert id="saveUser" parameterType="com.yy.xitongguanli.po.User">
insert user(username,password) values(#{username},#{password})
</insert>
</mapper>
6.xitongguanli.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="UserDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="SqlSessionFactory" />
<property name="mapperInterface" value="com.yy.xitongguanli.dao.IUserDao" />
</bean>
<bean id="UserService" class="com.yy.xitongguanli.service.impl.UserServiceImpl">
<property name="userDao" ref="UserDao"/>
</bean>
<bean id="RegisterAction" class="com.yy.xitongguanli.action.RegisterAction" scope="request">
<property name="userService" ref="UserService"/>
</bean>
</beans>
6.【jdbc.properties】
到这里代码就写完了,以下是注册测试
首先打开Severs 然后在浏览器上进入准确的路径
接下来衣dory 123 注册一个
最后看看user表中是否注册成功
就这样。over