初步Struts1
初始Struts1
1.WHAT
Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间。如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的选择。它通过采用JavaScript/JSP技术,实现了基于Java_EE Web应用的MVC设计模式的应用框架,是MVC典设计模式中的一个经典产品。
2.WHY
为什么会有Struts呢?
大家看上述图片,我们平常用Servelt的时候,是不是也是这样做的呢?客户端发送请求,然后在Servelt端接受,通过条件来判断到底用那个Servelt来接受信息处理。如果客户端多了的话,会在Servelt端出现大量的条件判断语句。那么如何来解决这个问题呢?
3.去除条件判断
1.配置文件上场
为了除去Servelt端大量的条件判断,我们可以把客户端与Servlet端封装到配置文件中,详细的信息如下:
<span style="font-family:SimSun;font-size:18px;"><?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>
</span>
上面的path路径就代表,客户端表单提交的路径配置,Type代表的是具体那个Servlet来处理客户端的请求,name中的信息正好与上面的表单对应,这样的话,我们就可以直接把客户端提交过来的表单中的数据直接在Action中取到。
4.Struts1实战
客户端登陆界面
<span style="font-family:SimSun;font-size:18px;"><%@ 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">
<input type="submit" value="登录">
</form>
</body>
</html></span>
登录的ActionForm
<span style="font-family:SimSun;font-size:18px;">package com.bjpowernode.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/*
* 负责ActionForm 负责表单收集数据
*/
public class LoginActionForm extends ActionForm {
private FormFile myfile;
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;
}
}
</span>
登录的控制器处理请求
<span style="font-family:SimSun;font-size:18px;">package com.bjpowernode.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.validator.Form;
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
* 负责取得表单数据,调用业务逻辑,返回转向信息
*/
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/*LoginActionForm lafActionForm=(LoginActionForm)form ;
String usernameString=lafActionForm.getUsername();
String passwordString=lafActionForm.getPassword();
if("admin".equals(usernameString)&&"admin".equals(passwordString)){
//登录成功
return mapping.findForward("success");
}else {
//登录失败
return mapping.findForward("error");
}*/
LoginActionForm lafActionForm=(LoginActionForm)form ;
String usernameString=lafActionForm.getUsername();
String passwordString=lafActionForm.getPassword();
UserManager userManager=new UserManager();
try {
userManager.login(usernameString, passwordString);
} catch (UserNotFoundException e) {
e.printStackTrace();
request.setAttribute("msg", "用户不能找到,用户名称"+usernameString);
}catch (PasswordErrorException e) {
e.printStackTrace();
request.setAttribute("msg", "密码错误");
}
//转到错误页面
return mapping.findForward("error");
}
}
</span>
5.小结
首先不管Struts内部的执行流程,我们如果单纯的用的话,需要做的就是首先建立子的ActionForm和控制器,然后按照Struts的格式,在配置文件中配置Form与控制器的对应关系即可。上面只是一个简单的应用,更多的内容请详见以后的博客。
- 2楼u0105401061分钟前
- DRP的吗?
- 1楼huo0650003小时前
- 先看一下吧!提前了解