SSH之struts2——搭建struts2,以及简单的登陆功能测试

这里做一个简单的用户登录,用来介绍struts2搭建


搭建struts2需要准备使用的jar包,先到官网下载struts2:http://struts.apache.org/


在下载的文件找到 apps/struts2-blank.war


这里的 WEB-INF\classes 里有struts.xml ,这个文件要copy到工程目录的src下



文件夹 WEB-INF\lib 里的jar就是需要使用的struts2核心jar包

如下图所示:


将jar包放到工程lib下



要使用struts2需要在工程的WEB-INF/web.xml里配置一个filter

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" 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>strust2-login</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- struts2的加载程序入口filter -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<!-- 配置struts2的主类 -->
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<!-- 这是所有的请求都将经过struts2 -->
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>


在WebContent(WebRoot)下创建三个jsp页面。。。根据eclipse不同自行查看WebContent(WebRoot)


login.jsp 登陆页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>

	<form action="${ pageContext.request.contextPath }/login" method="post">
		<table>
			<tr><td><input type="text" name="username"></td></tr>
			<tr><td><input type="password" name="password"></td></tr>
			<tr><td><input type="submit" name="login"></td></tr>
		</table>
	</form>

</body>
</html>

success.jsp 成功页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>登录成功</h1>
</body>
</html>

failer.jsp 失败页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>登录失败</h1>
</body>
</html>

创建普通类,并编写简单校验输入的信息。LoginAction

这里的校验在实际开发中是使用数据库校验,这里不作介绍。

package com.test.Action;
/**
 * 登陆功能的校验
 * 这里使用pojo类
 * @author admin
 *
 */
public class LoginAction {
	/**
	 * 接收页面请求过来的参数username,password
	 */
	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;
	}

	/**
	 * 输入信息简单校验
	 */
	public String login(){
		if("abc".equals(username) && "123".equals(password)){
			//登录成功
			return "success";
		}else{
			//登录失败
			return "error";
		}
	}
}

修改配置文件src\struts.xml

将标签<struts>里的所有内容删除,重新按下面的代码书写

<?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>
	 <!-- package
	    name 指定包名,包名必须唯一
	 	namespace 指页面访问地址是从工程根目录开始,通常和标签action里的name一起使用。
	 				例如下面这个最后访问的路径:http://localhost:8080/struts2-login/login
	 				namespace :/struts2-login/
	 				action的name:/login -->
    <package name="default" namespace="/" extends="struts-default">
		<!-- class 指的是刚才写的LoginAction类路径的全路径  method 指的是需要执行的方法 -->    
        <action name="login" class="com.test.Action.LoginAction" method="login">
        	<!-- result name 指的是login方法返回的字符串所匹配的值,success或error    type redirect 是指请求的方式是重定向 -->
            <result name="success" type="redirect">/success.jsp</result>
            <result name="error" type="redirect">/failer.jsp</result>
     
        </action>
    </package>

</struts>

使用tomcat启动



已经跳转到成功页面




登陆失败试验




跳转失败页面




请大家多多关照:QQ183428067




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值