struts实现的简单的登录
关于struts实现基本的登录步骤:
1.1导入相关的jar包
注:将这些包放入WEB-INF的lib包下,然后将包导入项目中
2.对WEB-INF包下的web.xml进行配置,引入过滤器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" version="2.5"> 3 <display-name></display-name> 4 <filter> 5 <filter-name>struts2</filter-name> 6 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 7 </filter> 8 <filter-mapping> 9 <filter-name>struts2</filter-name> 10 <url-pattern>/*</url-pattern> 11 </filter-mapping> 12 <welcome-file-list> 13 <welcome-file>index.jsp</welcome-file> 14 </welcome-file-list> 15 </web-app>
3.配置好web.xml之后,在src目录下创建struts.xml文件
4.接下来就是配置struts.xml文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!-- extends必须写,直接或者间接继承struts-default namespace从逻辑 8 上实现了action的模块化管理--> 9 <package name="login" extends="struts-default" namespace="/"> 10 <!-- name是请求名称;class对应action的路径名称完全限定名=包名+类名 --> 11 <action name="login" class="com.sxt.action.Login"> 12 <!-- result是结果集 name和对应action中的返回值匹配 默认是success --> 13 <result name="success">/index.jsp</result> 14 </action> 15 </package> 16 </struts>
5.通过上续的操作,准备步骤就做的差不多了,接下来就是实现实体类的封装,代码如下:
1 package com.sxt.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 @SuppressWarnings("serial")
<!--继承父类ActionSupport--> 6 public class Login extends ActionSupport{ 7 private String username; 8 private String password; 9 public String getUsername() 10 { 11 return username; 12 } 13 public void setUsername(String username) 14 { 15 this.username = username; 16 } 17 public String getPassword() 18 { 19 return password; 20 } 21 public void setPassword(String password) 22 { 23 this.password = password; 24 } 25 //对值进行判读,如果不是设定的值,则报错,这里自己可以随意赋值 26 public String execute() throws Exception 27 { 28 if("hello".equals(this.getUsername().trim())&&"word".equals(this.getPassword().trim())) 29 { 30 return "success"; 31 } 32 else 33 { 34 this.addFieldError("username", "username or password error!!!!"); 35 return "failer"; 36 } 37 } 38 @Override 39 public void validate() { //验证 40 41 if(null==this.getUsername()|| "".equals(this.getUsername().trim())) 42 { 43 this.addFieldError("username", "username request"); 44 } 45 if(null==this.getPassword()||"".equals(this.getPassword().trim())) 46 { 47 this.addFieldError("password", "password request"); 48 } 49 } 50 51 }
6.添加login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <%@taglib prefix="s" uri="/struts-tags" %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'main.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <s:form action="login"> 27 <s:textfield name="username" label="username"></s:textfield> 28 <s:password name="password" label="password"></s:password> 29 <s:submit name="submit"></s:submit> 30 </s:form> 31 </body> 32 </html>
7.添加main.jsp进行输出
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 username:${requestScope.username}<br> 25 password:${requestScope.password} 26 </body> 27 </html>
8.运行结果如图:
此前我判定的账号密码是hello和word,所以此处输入hello和word,对应的如果不是自己设定的值,则会报错,运行结果如下:
一个简单的登录注册就完成了!有问题欢迎与各位探讨,这里只是实现简单的登录注册,没有连接数据库,但是自己可以试下。