structs小程序

本文介绍了一个使用Struts2框架实现的简单登录系统案例。包括了login.jsp用于输入用户名和密码,LoginAction.java作为业务处理类验证用户信息,以及通过struts.xml配置文件定义流程。演示了dispatcher和redirect两种跳转方式的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


===========================================
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<title></title>
</head>
<body>
<form action="LoginAction.action" method="post">><!--默认是get-->

name<input type="text" name=username /><!--username后面有空格-->
password<input type="password" name=password />
<input type="submit"/>
<input type="reset"/>
</form>
</body>
</html>
===========================================
web.xml(路径在web-inf下)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
===========================================
struts.xml(路径在web-inf\classes下)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="LoginAction" class="com.zyl.action.LoginAction">
<result name="success" type="dispatcher">/success.jsp</result>
<result name="login" type="redirect">/login.html</result>
</action>
</package>
</struts>
<!-- 1.跳转方式
dispatcher和chain是服务器端跳转,所以客户端只发起一次请求,产生一个action;
redirect和redirectAction是客户端跳转,所以客户端发起两次请求。
2.跳转内容
dispatcher和redirect跳转的是views(一般是jsp页面);
chain和redirectAction跳转的是一个action。-->
===========================================
LoginAction.java
package com.zyl.action;


public class LoginAction {
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 execute(){
if("zyl".equals(username)&&"123".equals(password)){
return "success";
}else{
return "login";
}

}
}
===========================================
success.jsp
<%@page contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%><!-=这里是s不是p-->

<s:property value="username"/><!--这里是value 不是name-->

<%out.println("成功");%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值