1. 新建一个web project,
手动导入包:
D:\Java\jar\struts-2.3.24.1\apps\struts2-blank\WEB-INF\lib copy到 WEB-INF/lib下
D:\Java\jar\struts-2.3.24.1\apps\struts2-blank\WEB-INF\src\java下的struts.xml copy到src下进行修改:
/WEB-INF/jsp/error.jsp login_success.jsp login_failure.jsp
web.xml配置:
Struts2_1900_OGNL struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* login.jsp
Myeclipse配置的话:
右键项目->myeclipse->add struts...-> 下面选择*.action, *.do, /*, 选择最后一个
然后下一步选择core那个包, 会自动配置struts.xml和web.xml
2. WebRoot下新建一个login.jsp:
<%@ page language="java" import="java.util.*"%><%@ page contentType="text/html; charset=UTF-8" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>My JSP 'login.jsp' starting page 系统登陆
3. 新建登录成功页面和失败页面 login_success.jsp, login_failure.jsp
<%@ page language="java" import="java.util.*"%><%@ page contentType="text/html; charset=UTF-8" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>My JSP 'login.jsp' starting page 登陆成功
<%@ page language="java" import="java.util.*"%><%@ page contentType="text/html; charset=UTF-8" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>My JSP 'login.jsp' starting page 登陆失败
返回
4. 视图层完毕, 下面是模型层: vo, dao,action
vo层: new package: net.nw.vo,
new class:Users.java:
package net.nw.vo;public class Users { 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; }}
5. dao类:
new package: net.nw.dao,
dao类: UsersDAO.java:
package net.nw.dao;import net.nw.vo.Users;public class UsersDAO { public boolean usersLogin(Users u){ if(u.getUsername().equals("admin")&&u.getPassword().equals("123456")){ return true; } else{ return false; } }}
6. action
vo层: new package: net.nw.action
new class:LoginAction
package net.nw.action;import net.nw.dao.UsersDAO;import net.nw.vo.Users;public class LoginAction { public String username; public 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; } public String execute(){ Users u=new Users(); u.setUsername(this.getUsername()); u.setPassword(this.getPassword()); UsersDAO dao=new UsersDAO(); if(dao.usersLogin(u)){ return "login_success"; } else{ return "login_failure"; } }}
配置struts如第一步.