Struts视频笔记:
Struts是一个开源的web框架,框架提高了程序的规范的同时也约束了程序员的自由
为什么会有struts:因为我们队mvc理解的不同,可能造成不同公司写程序的时候,规范不统一,这样
不利于程序的维护和扩展,所以我们有必要用一个统一的规范来开发项目(struts)
Struts 的好处: 程序更加规范化,开发效率提高了,可读性增加了,程序的可维护性增加了
运行原理:
一个请求从浏览器发送给web服务器,http://localhost:8080/web应用/action,web服务器首先解
析主机然后解析web应用的名称在解析出资源名转发给总司令ActionServlet(该类由struts
框架提供给我们的无需编写,只需配置)ActionServlet有一个文件struts-config.xml,该文件配置了
表单actionForm(军火库),还配置了action,以及他们之间的对应关系,当ActionServlet拿到命令后它
会查询struts-config.xml文件去填充数据,把用户的数据填充到表单里边,下个动作就是去调用
指定的action(小队长),action去从表单中读取数据,调用某个model(士兵,如service)完成任务,
完 成 任 务 把 结 果 返 回 给 ActionServlet 总 司 令 ( 返 回 一 个 执 行 的 结 果 ),--> 总 司 令 又 去 查 询
struts-config.xml文件,决定跳转到哪个jsp页面,返回一个执行结果(形成静态html文件)直接返
回给web服务器服务器再把静态页面以http响应给浏览器
,登 录 小项 目 过 程 步骤: 新 建 w e b工 程 导 入struts 包 编 写login.jsp 编 写actionForm和
action配置struts-config.xml编写ok.jsp和err.jsp 配置web.xml 测试
Struts.config.xml中的
中的scope指的是actionform的生命周期范围 struts中的
scope默认是session
配置过滤器
public class MyFilter extends HttpServlet implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("gb2312");
arg1.setCharacterEncoding("gb2312");
arg2.doFilter(arg0, arg1);
}
配置web.xml
MyFilter
com.chao98.services.MyFilter
MyFilter
/*
上面这次比较浪费资源每次都要去实例化 但是下面这种过滤器不太彻底,往数据库里插入数据
时还是会经常出现乱码
public class MyFilter extends HttpServlet implements Filter {
private String encoding;
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// arg0.setCharacterEncoding("gb2312");
//arg1.setCharacterEncoding("gb2312");
arg0.setCharacterEncoding(encoding);
arg2.doFilter(arg0, arg1);
}
}
然后在web.xml中加入
public void init(FilterConfig arg0) throws ServletException {
encoding=arg0.getInitParameter("encoding");