简单的Struts2登录功能流程

开发环境

eclipse、jdk1.7.0_55、tomcat7.0.54、struts2.3.16

下载struts2全jar包

进入下载地址:http://struts.apache.org/download.cgi

如果需要其他的版本则下拉页面到Prior releases

下载后解压得到

 一般只需要下载下来的jar包中lib文件夹中的commons-fileuploed-1.3jar、freemarker-2.3.19、ognl-3.0.6.jar、struts2-core-2.3.16.jar、xwork-core-2.3.16等文件

功能流程图

流程说明:

1.通过浏览器,运行登录界面,单击“登录”按钮,向服务器提交用户输入的用户名和密码信息。

2.读取web.xml配置文件,加载Struts2的核心控制器StrutsPrepareAndExecuteFilter,对用户请求进行拦截。

3.根据用户提交表单中的Action,在struts.xml配置文件中查找匹配相应的Action配置,这里会查找name属性值为login的Action配置,并且把已经拦截的请求发给相对应的LoginAction业务类来处置。

4.在struts.xml配置文件中没有指定Action元素的method属性值,此时,系统会调用默认方法execute()来完成对客户端的登录请求处理。若登录成功,则返回success字符串,否则返回input字符串

5.根据返回结果,在struts.xml配置文件中查找相应的映射。在struts.xml文件中配置LoginAction时,指定了<result name="success">/success.jsp</result>,因此,当LoginAction类的execute()方法返回“success”字符串时,则 转向success.jsp页面,否则转向login.jsp页面。

功能实现

1.新建Web项目LoginDemo

2.添加Struts2jar包

把下载下来的jar包中commons-fileuploed-1.3jar、commons-logging-1.1.3.jar、commons-io-2.2.jar、commons-lang3-3.1.jar、freemarker-2.3.19、javassist-3.11.0.GA.jar、ognl-3.0.6.jar、struts2-core-2.3.16.jar、xwork-core-2.3.16等文件复制到Web应用程序中的WebRoot/WEB-INF/lib路径下即可,然后刷新项目

3.新建jsp页面 

在WebRoot根目录下创建login.jsp登录页面和success.jsp成功页面

 login.jsp(登录界面)

<%@ page language="java" import="java.util.*"  pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %><!-- 导入struts2标准库 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录界面</title>
</head>
<body>
	<!-- 提交请求 -->	
	<s:form name="form1" action="login"  method="post">
		<s:textfield name="username" label="用户名"></s:textfield>
		<s:textfield name="password" label="密 码"/>
		<s:submit value="登录"/>
	</s:form>
</body>
</html>

success.jsp(成功界面)

<%@ page language="java" import="java.util.*"  pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %><!-- 导入struts2标准库 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录成功界面</title>
</head>
<body>
	<div>
		欢迎<s:property value="username"/>,登录成功。
	</div>
</body>
</html>

 4.配置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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Logindemo</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-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<!-- 过滤器拦截 -->
  	<filter-name>struts2</filter-name>
  	<!-- 配置Strust2的核心过滤器拦截所有的用户请求 -->
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

5.在src文件下新建LoginAction类(Action业务类)

package com.yzpc.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	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;
	}

	@Override
	public String execute() throws Exception{
//账号为:admin密码:为123,正确则返回字符串success,错误则返回input
		if ("admin".equals(username)&&"123".equals(password)) {
			return SUCCESS;
		}else {
			return INPUT;
		}
	}
}

6、编写struts.xml配置文件 

在项目的src文件夹下创建struts.xml文件

<?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="default" namespace="/" extends="struts-default">
		
		<action name="login" class="com.yzpc.action.LoginAction">
			<result name="success">/success.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
		
	</package>
</struts>

 效果图展示

登录界面

成功界面

 总结

经过项目的实施,对struts2的mvc思想的具体实现和基本流程和相关组件有了更详细的了解

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2框架是一个用于开发Web应用程序的Java框架,它基于MVC设计模式,并提供了一套丰富的标签库和插件,能够简化Web应用程序的开发过程。 Struts2框架的基本流程如下: 1. 环境准备:首先需要在开发环境中配置好Java开发工具(如Eclipse、IntelliJ IDEA等)、Tomcat服务器和Struts2框架的相关jar包。 2. 创建项目:在开发工具中创建一个新的Web项目,并将Struts2框架的相关jar包引入到项目中。 3. 配置web.xml:在web.xml文件中配置Struts2框架的过滤器,以便让其能够拦截并处理所有的请求。 4. 创建Action类:编写Action类,它将负责处理Web应用程序中的各种请求,并将处理结果返回给用户。 5. 创建视图:编写JSP页面或FreeMarker模板,用于显示页面内容并接收用户的输入。 6. 配置struts.xml:在struts.xml文件中配置Action类的映射关系,将URL与相应的Action类方法进行映射。 7. 编写业务逻辑:在Action类中编写业务逻辑,可以调用服务层或数据库访问层的方法来实现具体的功能。 8. 联合视图:将Action类处理的结果通过视图呈现给用户,可以使用标签库和插件来达到更好的效果。 9. 测试和调试:在本地服务器上运行Web应用程序,进行测试和调试,确保各个模块都能够正常运行。 10. 部署上线:将Web应用程序部署到生产环境的服务器上,以便用户可以访问和使用。 总的来说,Struts2框架的开发流程就是配置环境、创建项目、编写Action类和视图、配置映射关系、编写业务逻辑、联合视图、测试调试和部署上线这几个步骤。通过这些步骤,我们可以快速高效地开发出一个功能完善、性能稳定的Web应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值