上周我们学习了struts2+spring+mybetis之后,我们做了一个小小的登录注册项目。在接触了struts2之后,觉得做这个好方便啊。查询什么的代码都是写好的,我们不用在java里面写查询什么的代码。学了struts后,生活变得很方便了。呵呵,小高兴一下下。
我们做的登录注册项目其实说难也不难,说简单也不简单。对于初学者来说有点难度,思维转的要比平时的要快,java里面逻辑思维最重要,手是跟着思维动的。
这个项目中的难点(我认为):
1、页面的跳转
2、方法的调用
3、session的应用
4、action中方法的写法
下面是我做这个项目的重要部分代码代码,给初学struts+spring+mybetis的童鞋参考下:
<loginAction.java>
package com.doctor.admin.login.action;
import com.doctor.admin.city.*;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.doctor.admin.login.service.loginService;
import com.doctor.city.model.City;
import com.doctor.city.model.CityExample;
import com.doctor.city.model.CityOfMan;
import com.doctor.city.model.CityOfManExample;
import com.doctor.common.base.BaseAction;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;
@Controller
@Scope("prototype")
public class loginAction extends BaseAction {
@Resource(name = "loginService")
loginService loginService;
// 实例化City
protected CityOfMan cityofman;
protected List<CityOfMan> cityman;
// 实例化loginExample对象
CityOfManExample loginExample = new CityOfManExample();
private static final long serialVersionUID = -6612930633666796678L;
private static final Logger LOGGER = Logger.getLogger(loginAction.class);
// 预登录操作
public String toLogin() {
return "toLogin";
}
// 登录验证操作
public String doLogin() {
String name = getParameter("name");
String pwd = getParameter("pwd");
cityman = loginService.SelectManList(loginExample);
for (int i = 0; i < cityman.size(); i++) {
if (name.equals(cityman.get(i).getName())) {
if(pwd.equals(cityman.get(i).getPwd().toString())){
this.setSession("loginName", cityman.get(i).getName());
return "cityList";//匹配的进入city页面
}
}
}
return "AddUser";//不匹配的重新进入注册页面
}
// 准备注册user
public String AddUser() {
return "AddUser";
}
// 注册操作
public String doUserAdd() {
try
{
loginService.insertSelective(cityofman);
this.setSession("loginName", cityofman.getName());
}
catch (Exception e)
{
e.printStackTrace();
}
return "doUserAdd";
}
//退出操作
public String logout()
{
// 方法日志入口
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Enter String logout()");
}
if (getSession() != null)
{
//清除session
getSession().clear();
}
// 方法日志出口
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Exit String logout()");
}
return "cityList";
}
public CityOfManExample getLoginExample() {
return loginExample;
}
public void setLoginExample(CityOfManExample loginExample) {
this.loginExample = loginExample;
}
public CityOfMan getCityofman() {
return cityofman;
}
public void setCityofman(CityOfMan cityofman) {
this.cityofman = cityofman;
}
public List<CityOfMan> getCityman() {
return cityman;
}
public void setCityman(List<CityOfMan> cityman) {
this.cityman = cityman;
}
}
<struts-login.xml>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" namespace="/login" extends="basePackage">
<action name="login" class="loginAction">
<result name="toLogin" type="velocity">
/WEB-INF/systemManage/Login.vm
</result>
<result name="AddUser" type="velocity">
/WEB-INF/systemManage/Register.vm
</result>
<result name="doUserAdd" type="velocity">
/WEB-INF/systemManage/City.vm
</result>
<result name="cityList" type="velocity">
/WEB-INF/systemManage/City.vm
</result>
<!-- 重复提交跳转页面
<result name="invalid.token" type="velocity">
/WEB-INF/common/ajaxDone.vm
</result>-->
</action>
</package>
</struts>
<Login.vm>
#**
文件名:home.vm
版权:Copyright 2011 EPRO Tech. Co. Ltd. All Rights Reserved.
描述:用户登录
修改人:Liu Bo
*#
#set($path=$request.getContextPath())
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style type="text/css">
.btnl{display:inline-block;width:80px;height:35px;line-height:36px;font-size:17px;color:gray;text-decoration:none}
</style>
<script type="text/javascript">
function validate()
{
var addForm=document.getElementById("loginForm");
addForm.action="$path/login/login!doLogin.do";
addForm.submit();
}
</script>
</head>
<body>
<div class="content">
<div class="content_resize">
<div class="mainbar"><div class="submb">
<div class="article">
<center>
<h2>请登录</h2>
<form action="" method="post" id="loginForm">
<ol>
<li>
用户号:
<input id="name" name="name" type="text" />
</li>
<br/>
<li>
密码:
<input id="pwd" name="pwd" type="password" />
</li>
<br/>
<span><a href="${path}/login/login!AddUser.do">没有账号?请点这儿</a></span>
<li>
<br/>
<a href="$path/city/city!SelectCityList.do"><input type="button" value="登录" class="btnl" οnclick="validate()"></a>
<input type="reset" value="取消" class="btnl">
</li>
</ol>
</form>
</center>
</div>
</div></div>
<div class="clr"></div>
</div>
</div>
</body>
</html>
<Register.vm>
#**
文件名:home.vm
版权:Copyright 2011 EPRO Tech. Co. Ltd. All Rights Reserved.
描述:修改员工信息
修改人:Liu Bo
*#
#set($path=$request.getContextPath())
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style type="text/css">
/************jQuery.Validate插件样式开始********************/
label.error {
background: url(images/error.png) no-repeat 0px 0px;
color: Red;
padding-left: 20px;
display: none;
}
input.error
{
border: dashed 1px red;
}
/************jQuery.Validate插件样式结束********************/
</style>
<style type="text/css">
.btnl{display:inline-block;width:145px;height:40px;line-height:36px;font-size:17px;color:gray;text-decoration:none}
</style>
<script type="text/javascript">
function validate()
{
var addForm=document.getElementById("registerForm");
addForm.action="${path}/login/login!doUserAdd.do";
addForm.submit();
}
</script>
</head>
<body>
<form method="post" id="registerForm">
<center><h2>--请注册--</h2>
<br>ID:<input id="reID" name="cityofman.id" type="text" /></br>
<br>用户名:<input id="rename" name="cityofman.name" type="text" /></br>
<br>密码:<input id="repwd" name="cityofman.pwd" type="password" /></br>
<br/>
<li>
<a href="$path/city/city!SelectCityList.do"> <input type="button" value="注册" class="btnl" οnclick="validate()"></a>
</li>
</center>
</form>
</body>
</html>