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下进行修改:
<?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> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="login" class="net.nw.action.LoginAction"> <result name="login_success">login_success.jsp</result> <result name="login_failure">login_failure.jsp</result> </action> </package> </struts>
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2_1900_OGNL</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘login.jsp‘ starting page</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> <center> <h1>系统登陆</h1> <hr> <form name="loginForm" action="<%=path%>/LoginServlet" method="post"> 用户名: <input type="text" name="username"/><br> 密码 : <input type ="text" name="password"/><br> <input type="submit" value="登陆"/><br> </form> </center> </body> </html>
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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘login.jsp‘ starting page</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> <center> <h1>登陆成功</h1> <hr> </center> </body> </html>
<%@ 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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘login.jsp‘ starting page</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> <center> <h1>登陆失败</h1> <a href="<%=path%>/login.jsp">返回</a> <hr> </center> </body> </html>
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如第一步.
时间: 2024-10-13 02:21:16