Struts1(一)——基本原理

MVC框架:


通过Servlet实现MVC:



通过struts1实现MVC:




细粒度的时序图:


一个登陆的小例子:


在lib中复制粘贴相应的jar包,编写jsp页面

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="text" name="password"><br>
		<input type="submit" value="登录">
	</form>
</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>
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>
LoginAction.java:

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;

/**
 * 登录Action
 * 负责取得表单数据、调用业务逻辑、返回转向信息
 * @author cnwl
 *
 */
public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf= (LoginActionForm)form;
		String username = laf.getUsername();
		String password = laf.getPassword();
		if("admin".equals(username) && "admin".equals(password)){
			//登陆成功
			return mapping.findForward("success");
			
		}else{
			//登录失败
			return mapping.findForward("error");
		}
	}
	
}
LoginActionForm.java

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;

/**
 * 登录Action
 * 负责取得表单数据、调用业务逻辑、返回转向信息
 * @author cnwl
 *
 */
public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf= (LoginActionForm)form;
		String username = laf.getUsername();
		String password = laf.getPassword();
		if("admin".equals(username) && "admin".equals(password)){
			//登陆成功
			return mapping.findForward("success");
			
		}else{
			//登录失败
			return mapping.findForward("error");
		}
	}
	
}
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>  
</web-app>
struts-config.xml:

<?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-beans>
	
	<action-mappings>
		<action path="/login" 
				type="com.bjpowernode.struts.LoginAction"
				name="loginForm" 
				scope="request">
			<forward name ="success" path="/login_success.jsp"/>
			<forward name ="error" path="/login_error.jsp"/>
		</action>
	</action-mappings>
</struts-config>
在浏览器中输入:http://localhost:8080/struts_login/login.jsp

登陆页面:


登录成功:


登录失败:


总结

       通过以上几张图的详细讲解,基本上MVC原理就是这样,终于了解到struts原理是怎样的。根据URL截取到相关数据,通过一系列操作把表单中的数据传到actionForm中,然后创建响应的action,然后响应到视图。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值