Struts2快速入门

Struts2与Struts1没有太多的关系。

Struts1最大的特点就是提供了JSP标记库以及页面导航。

Struts2是从WebWork框架发展起来的。Struts2兼具Struts1和WebWork的优点。


Struts2的基本工作过程:

1、客户端向服务器端提交请求,容器初始化HttpServletRequest请求对象。

2、请求对象被一系列的Servlet过滤器过滤。

Struts2过滤器有三种:

①ActionContextCleanUp过滤器:是一个可选的过滤器,主要用来集成其他框架。

②其他插件的核心过滤器:如SiteMesh插件的过滤器

③FilterDispatcher过滤器:是Struts2API中提供的过滤器,必须使用。

3、FilterDispatcher过滤器调用ActionMapper,决定该请求是否需要调用某个Action。

4、如果请求需要调用某个Action,ActionMapper将通知FilterDispatcher过滤器把请求的处理交给ActionProxy来处理。

5、ActionProxy通过Configuration Manager解析框架的配置文件struts.xml,找到需要调用的Action类。

6、ActionProxy将创建一个ActionInvocation实例。

7、ActionInvacation实例使用命令模式回调Action中的exectue方法,Action调用业务逻辑类完成业务逻辑。在调用Action的前后,将调用该Action涉及的相关拦截器(Interceptor)。

8、Action执行完毕后,ActionInvocation根据struts.xml中的配置找到对应的返回结果(成为Result)。返回结果通常是JSP、FreeMarker等模板文件。

实例:验证登陆

1、使用java类实现Model层的登录逻辑

package Model;

public class LoginService {

	public boolean login(String custname,String pwd){
		if(custname.equals("ETC")&&pwd.equals("123")){
			return true;
		}else{
			return false;
		}
	}
}
2、编写index.jsp,用来输入用户名和密码进行登录

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="Login">
<s:textfield name="custname" label="Input your custname"></s:textfield><br>
<s:password name="pwd" label="Input your password"></s:password><br>
	<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

3、编写welcome.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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome,${param.custname}
</body>
</html>
4、创建LoginAction类,调用Model中的登录逻辑,根据登录结果不同而返回不同的结果

package Action;

import Model.LoginService;

public class LoginAction {

	private String custname;
	private String pwd;
	public String getCustname() {
		return custname;
	}
	public void setCustname(String custname) {
		this.custname = custname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String execute(){
		LoginService ls=new LoginService();
		boolean flag=ls.login(custname, pwd);
		if(flag){
			return "success";
		}else{
			return "fail";
		}
	}
}
5、配置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="com.etc.action" extends="struts-default">
    	<action name="Login" class="Action.LoginAction">
    		<result name="success">/welcome.jsp</result>
    		<result name="fail" >/index.jsp</result>
    	</action>
    </package>
</struts>

6、配置web.xml的FilterDispatcher

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <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>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值