struts1 知识总结

  学到现在,发现不会的越来越多,想学的也越来越多。在JAVA世界里,技术层出不穷。要是一样一样学过去,估计这辈子就过去了。那些老师们说得很对,我们要学习那些不变的地方,往往这些地方就是技术的根本,从而以不变应万变。学习struts1的时候一直都觉得学习它的思想最重要。下面对我这三天的学习内容做一个总结。


1.struts是什么

 struts的目标是提供一个开发Web应用的开源框架,struts鼓励基于M2模式(即MVC设计模式)来开发程序。


2.基本原理图

     

  我们可以对这张图中的各个节点与MVC中的概念相对应起来。节点JSP就是View,表现层;节点ActionServlet可以理解为Controller,在struts中ActionServlet已经为我们做好了封装,至于如何转向,由什么类处理我们只要在配置文件中进行描述就可以了。这个配置文件就是struts-config.xml;节点Model+节点Action可以理解为MVC中的Model,java中可以看做是Java-bean。

  流程描述图:



3.重要知识点

  ActionServlet:该类控制导航流。

  ActionForm:是一个JavaBean,需继承org.apache.struts.action.ActionForm类,它捕获通过HTTP请求传送的参数。它针对每个HTML表单中的字段具有一个对应的属性。ActionServlet匹配请求中的参数和ActionForm中的属性,并调用ActionForm中的setter方法,将参数传入ActionForm。这个类还可以帮助我们做好数据类型的转换。

  ActionForward:Action在完成业务逻辑后,返回一个ActionForward 对象,ActionServlet根据ActionForward对象中的路径来调用页面完成响应

  Struts将这些信息绑定在一个ActionMapping对象中,一个ActionMapping对应一个请求URI,当请求路径到达的时候,ActionServlet就会查询ActionMapping对象,ActionMapping对象将告诉ActionServlet哪个Action类会被调用、哪个ActionForm类被用于传递页面数据以及哪些ActionForward将被用于转向

  有关Action、ActionForward、ActionForm的信息都是通过配置文件来定义的。


4.login简单实例的实现

  struts只能再web项目中使用。使用的时候我们需要先引入struts的jar包到WEB-INF/lib文件夹下。如图:


然后在web.xml文件下添加如下配置

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

把struts-config.xml文件放在WEB-INF/lib文件下:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.bjpowernode.struts.loginActionForm"></form-bean>
	</form-beans>
	
	<action-mappings>
		<action path="/login"
				type="com.bjpowernode.struts.loginAction"
				name="loginForm"
				scope="request"
				>
			<forward name="success" path="/login_success.jsp"></forward>
			<forward name="error" path="/login_error.jsp"></forward>
		</action>
	</action-mappings>
</struts-config>

具体代码:

view层:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
     <a href="login.jsp">登录</a>
  </body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	<form action="login.do" method="post">
		用户:<input type="text" name="username" /> <br>
		密码:<input type="password" name="password" /> <br>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>
login_success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	登录成功!
</body>
</html>
login_error.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	登录失败!
</body>
</html>


package:com.bjpowernode.struts

LoginActionForm类:

package com.bjpowernode.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class loginActionForm 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;
	}
	
	
}
LoginAction类:

package com.bjpowernode.struts;

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;

/**
 * 
 * @author wyj
 *
 */
public class loginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		loginActionForm lgf = (loginActionForm)form;
		
		String username = lgf.getUsername();
		String password = lgf.getPassword();
		
		if("admin".equals(username) && "admin".equals(password)) {
			return mapping.findForward("success");
		}else{
			return mapping.findForward("error");
		}
	}
	
}

 这个login的小例子非常简单,没有Model层。但是非常适合刚入门学习struts的人。

5.总结:这只是struts入门级别的宏观总结。struts是一个开源的MVC框架。它帮我们做好了封装,有助于提高开发效率。对“约定大于配置”这句话有了一些理解。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值