Struts2 Demo

实现步骤:

1、添加依赖jar包

2、编写login.jsp文件、success.jsp文件、failure.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="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>
<title>LOGIN</title>
</head>

<body>
	<form action="<%=request.getContextPath()%>/login.action" method="post">
		户名:<input type="text" name="username"><br> 密码:<input
			type="password" name="password"><br> <input
			type="submit" value="login">
	</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="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>
<title>SUCCESS</title>
</head>

<body>登录成功!!!
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="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>
<title>FAILURE</title>
</head>

<body>
	登录失败,错误的用户名:
	<s:property value="username" />
	<br>
	<a href="<%=request.getContextPath()%>/login.jsp">返回</a>
</body>
</html>

3、编写LoginAction.java文件

package com.jack.action;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {
	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() throws Exception {
		if (username.equals("struts2")) {
			return "loginSuccess";
		} else {
			return "loginFailure";
		}
	}
}

4、编写struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="myFirst" namespace="/" extends="struts-default">
		<action name="login" class="com.jack.action.LoginAction">
			<result name="loginSuccess">/success.jsp</result>
			<result name="loginFailure">/failure.jsp</result>
		</action>
	</package>
</struts>

5、编写web.xml文件

<?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">
	<display-name></display-name>
	<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-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

6、测试,访问链接:http://localhost:8080/hellostruts2/login.jsp

121740_EXC3_2365309.jpg

121740_XV8h_2365309.jpg

121740_b7Aw_2365309.jpg


转载于:https://my.oschina.net/zhanggongming/blog/671366

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Struts2 示例,将输入的名称传递给 Action 类并在 JSP 页面上显示欢迎消息。 首先,我们需要创建一个名为 "WelcomeAction" 的 Action 类,在其中定义一个名为 "name" 的属性和一个名为 "execute" 的方法。 ```java package com.example.struts2demo; public class WelcomeAction { private String name; public String execute() { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 接下来,我们需要创建一个名为 "welcome.jsp" 的 JSP 页面,该页面将显示欢迎消息,并使用 Struts2 标记库获取 Action 类中设置的名称。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome <s:property value="name" /></h1> </body> </html> ``` 接下来,我们需要创建一个名为 "struts.xml" 的配置文件,该文件将指定如何将请求映射到 Action 类以及如何处理结果。 ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" extends="struts-default"> <action name="welcome" class="com.example.struts2demo.WelcomeAction"> <result name="success">/welcome.jsp</result> </action> </package> </struts> ``` 最后,我们需要创建一个名为 "index.jsp" 的 JSP 页面,该页面将显示一个表单,允许用户输入他们的名称,并将其作为参数传递给 Action 类。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome</h1> <s:form action="welcome"> <s:textfield name="name" label="Enter your name" /> <s:submit value="Submit" /> </s:form> </body> </html> ``` 现在,我们已经完成了 Struts2 示例的所有必要组件。要运行示例,请将这些文件保存在您的 Web 应用程序中,并使用您喜欢的 Web 服务器启动应用程序。然后,导航到 "index.jsp" 页面,输入您的名称并单击提交按钮,应该会显示一个欢迎消息,其中包含您输入的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值