Java框架(二)——Structs

       Structs是Java三大框架之一,Struts是采用JavaServlet/JavaServer Pages技术,开发Web应用程序的开放源码的Framework。采用Structs开发是基于MVC的应用框架。


       首先简单再说一下MVC(Model/View/Controller):

      M是指数据模型,在Structs中通常由ActionForm Bean表示,

      V是指用户界面,视图通常是由JSP建立的,Structs包含扩展自定义标签库(TagLib),可以简化用户界面的创建过程。目前的标签库包括:Bean Tags 、 HTML tags 、 Logic Tags 、 Nested Tags 以及 Template Tags 等。

      C则是控制器,在Structs中实现控制逻辑的是Action,在struts-config.xml配置文件中ActionMapping 与 ActionForward 则指定了不同业务逻辑或流程的运行方向。

      使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式,C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新。


       Structs只能用于Web程序开发,那么它的工作流程是怎么走的呢?在Struts中,用户的请求一般以*.do作为请求服务名,所有的*.do请求均被指向ActionSevlet,ActionSevlet根据Struts-config.xml中的配置信息,将用户请求封装成一个指定名称的FormBean,并将此FormBean传至指定名称的ActionBean,由ActionBean完成相应的业务操作,如文件操作,数据库操作等。每一个*.do均有对应的FormBean名称和ActionBean名称,这些在Struts-config.xml中配置。所以说Struts的核心是ActionSevlet,ActionSevlet的核心是Struts-config.xml。

      

      下面做一个登陆页面的小Demo和大家分享:

首先是JSP页面的代码(其中使用了taglib):

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
	<html:base />
	<title>登陆页面</title>
</head>

<body>
	<center>
		<font color="red"><html:errors property="loginerror" /></font><br>
		
		<form action="login.do" method="post"  focus="login">
			<table border="0">
				<tr>
					<td>用户名:</td>
					<td><input name="username" type="text" /></td>
					<td>
						<font color="red"><html:errors property="username"/></font>
					</td>
				</tr>
				<tr>
					<td>密码:</td>
					<td>
						<input name="password" type="password" />
					</td>
					<td>
						<font color="red"><html:errors property="password"/></font>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" value="提交"/>
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html:html>


接着是ActionServlet代码:


package com.sinosoft.servlet.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.sinosoft.servlet.form.LoginForm;

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		LoginForm loginForm=(LoginForm)form;
		if(loginForm.getUsername().equals("xdp") && loginForm.getPassword().equals("123")){
			//登陆成功,将用户信息设置到session中
			request.getSession().setAttribute("user", loginForm.getUsername());
			//跳转到指定的页面
			return mapping.findForward("success");
		}else{
			//登陆失败,处理
			ActionMessages errors=new ActionMessages();
			errors.add("loginerror",new ActionMessage("login.error"));
			this.addErrors(request, errors);
			//获取配置文件中action的input属性,并跳转到这个页面
			return mapping.getInputForward();
		}
	}
}


下面是ActionForm的代码:

package com.sinosoft.servlet.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginForm extends ActionForm {

	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 ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors=new ActionErrors();		
		//判断用户名,加入错误信息
		if(this.username==null || this.username.trim().equals(""))
		{
			errors.add("username", new ActionMessage("username.null"));
		}
		//判断密码,加入错误信息
		if(this.password==null || this.password.trim().equals(""))
		{
			errors.add("password", new ActionMessage("password.null"));
		}		
		return errors;
	}
}


我们还添加了一个配置文件ApplicationResources.properties,用来存放错误提示信息:



最后一个是struts-config.xml配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
	<data-sources />
  <form-beans>
  	<form-bean name="loginForm" type="com.sinosoft.servlet.form.LoginForm"/>
  </form-beans>
   <global-exceptions />
  <global-forwards />
  <action-mappings>
  	<action path="/login" attribute="loginForm" 
  		input="/login.jsp" name="loginForm" scope="request"
  		type="com.sinosoft.servlet.action.LoginAction">
  		<forward name="success" path="success.jsp"></forward>
  	</action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources"></message-resources>
</struts-config>





  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts Logic标签库中包含的标签列表 Tag name Description empty 如果标签parameter,propertie等属性所指定的变量值为null或空字符串,则处理标签包含的内容 equal 如果标签parameter,propertie等属性所指定的变量的值等于标签value属性所指定的值,则处理标签所包含的内容, 如: <logic:equal value="modify" property="action" name="projectForm"> <bean:message key="project.project_modify"/> </logic:equal> 上面的示例表示,如果projectForm的action属性等于modify,则处理<bean:message key="project.project_modify"/ >语句。 forward Forward control to the page specified by the ActionForward entry. greaterEqual Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if the requested variable is less than or equal to the specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值