public interface ActionManager {
/**
* 根据用户名,密码验证登录是否成功 * @param username 登录所输入的用户名 * @param password 登录的密码 */
Integer validLogin(String username , String password); }
2. 在com.service.impl包中创建业务逻辑层接口的实现类,如图7.
图7 ActionManager实现接口
package com.service.impl;
import com.dao.UserDAO;
import com.domain.Users;
import com.service.ActionManager;
public class ActionManagerImpl implements ActionManager{
public Integer validLogin(String username, String password){
try
Users user = userDAO.findUserByNameAndPass(username, if (user!= null) { }
return user.getId();
{
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) { }
this.userDAO = userDAO;
password);
}
catch (Exception e)
}
{
System.out.println(e.getMessage()); } }
return null;
四、
控制层设计与实现
1. Action的创建
(1)在com. action包中创建Action。如图8.
图8 登录Action
// LoginAction源码如下:
package com.action;
import com.service.ActionManager;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String execute() throws Exception {
}
Integer userId = mgr.validLogin(username, password);
if (userId != null) { }
return SUCCESS;
addActionError(\用户名/密码不匹配\); return \; } else {
return \;
protected ActionManager mgr;
private String username; private String password;
private static final long serialVersionUID = 1L;
if(username.equals(\) || username == null){
}
public String getPassword() { }
public String getUsername() { }
public void setPassword(String password) { }
public void setUsername(String username) { }
this.username = username; this.password = password; return username; return password;
public void setMgr(ActionManager mgr) {
this.mgr = mgr;
}
public ActionManager getMgr() {
return mgr;
} }
2. Struts2控制文件和属性文件的配置 (1) action的配置
系统生成的Struts.xml模板为:
//struts.xml
配置资源自后的Struts.xml为
//struts.xml
value=\ /> struts.properties // struts.propertise struts.locale=zh_CN struts.i18n.encoding=UTF-8 struts.objectFactory = spring struts.objectFactory.spring.autoWire = name struts.multipart.parser=jakarta struts.action.extension=action struts.serve.static=true struts.serve.static.browserCache=true struts.enable.DynamicMethodInvocation = true struts.enable.SlashesInActionNames = false struts.tag.altSyntax=true struts.devMode = false struts.i18n.reload=false struts.ui.theme=xhtml struts.ui.templateDir=template #sets the default template type. Either ftl, vm, or jsp struts.ui.templateSuffix=ftl struts.configuration.xml.reload=false ### Location of velocity.properties file. defaults to velocity.properties struts.velocity.configfile = velocity.properties ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContext struts.velocity.contexts = ### Location of the velocity toolbox struts.velocity.toolboxlocation= ### used to build URLs, such as the UrlTag struts.url.http.port = 80 struts.url.https.port = 443 ### possible values are: none, get or all
相关推荐: