Struts2搭建登录页面

来源:互联网 发布:windows安全性 编辑:程序博客网 时间:2024/06/07 23:13

项目路径:


1、创建web应用,导入相关jar包


2、配置web.xml文件

<?xml version="1.0" encoding="GBK"?><!-- 配置web应用配置文件的根元素,并且指定配置文件的schema信息 --><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><!-- 定义Struts2的核心控制器:StrutsPrepareAndExecuteFilter --><filter><!-- 定义核心Filter的名字 --><filter-name>struts2</filter-name><!-- 定义核心Filter的实现类 --><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- StrutsPrepareAndExecuteFilter用来处理所有的HTTP请求 --></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
3、创建struts.xml

<?xml version="1.0" encoding="GBK"?>  <!DOCTYPE struts PUBLIC      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"      "http://struts.apache.org/dtds/struts-2.3.dtd">  <!-- 配置struts2的配置文件的根元素 -->  <struts>    <!-- Struts2的Action必须放在指定的包空间下定义 -->      <package name="test" extends="struts-default">    <!-- 定义login的Action -->        <action name="login" class="common.action.HelloWorldAction">              <!-- 定义处理结果和视图资源之间的映射关系 -->            <result name="error">error.jsp</result>              <result name="success">welcome.jsp</result>          </action>      </package></struts>
4、创建action类

package common.action;public class HelloWorldAction {private String name;private String password;public String execute() throws Exception{if(getName().equals("hello")&&getPassword().equals("world")){return "success";}else{return "error";}}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
5、创建登录页面loginForm.jsp

<%@ page contentType="text/html; charset=GBK"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>登录页面</title></head><body><form action="login" method="post"><table><caption><h3>用户登录</h3></caption><tr><td>用户名:<input type="text" name="name" /></td></tr><tr><td>密码:<input type="text" name="password" /></td></tr><tr align="center"><td colspan="2"><input type="submit" value="登录" /> <inputtype="reset" value="重填" /></td></tr></table></form></body></html>



6、登录成功页面

<%@ page language="java" contentType="text/html; charset=GBK"    pageEncoding="GBK"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>成功页面</title></head><body>您已经登录!</body></html>


7、登录失败页面

<%@ page language="java" contentType="text/html; charset=GBK"    pageEncoding="GBK"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>失败页面</title></head><body>请重新登录!</body></html>







原创粉丝点击