关键词:
前言
我是一个小白,今天首次学习Struts的基本内容,是看视频和看书相结合的,主要是记录这个学习的过程以及学习的过程中遇到的问题。
-----------------------------------------------------------------------------------------------------------------------------------
传统的MVC分为servlet(控制器),Javabean(模型层),jsp(显示层)三个部分,Struts可以说是MVC的一个具体的实现,增加了ActionForm、Action和Struts标签库层。接下来用myEclipse开发第一个Struts程序,大致流程为:在hello.jsp页面通过文本框输入要显示的内容,然后提交到struts,如果内容为空,点击显示按钮,出现错误提示;如果不为空,struts将信息显示在页面上。
file--->new--->webproject,这里将名称取为strutstest,然后点击此项目右键,--->myEclipse--->Add Struts Capabilities,这里是添加了struts支持 。
点击finish之后在项目下可以看到struts-config.xml web.xml JavaEE的jar包以及Struts的jar包等。
删除原来的index.jsp 在Web-Root下新建一个hello.jsp
接下来新建ActionForm及Action。注意每一个处理类Action都要绑定一个ActionFrom。
选中刚刚的com.du.struts包--->new --->others ,再搜索Struts,选择Struts1.3下的struts1.3 From,Action&JSP
点击next
点击finish之后,配置文件里面的form-beans和action-mappings有新的内容。其中path表示提交的路径,input表示错误信息的显示页面。在web.xml里面的url-pattern是*.do,那么在hello.jsp里面的action就是hello.do.大体框架已经搭好了,下面就是进行逻辑上的操作。我把各部分的代码放在下面供参考:
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 3 <!-- 这是Struts已经添加好的标签库,我们直接使用即可 --> 4 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 5 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 6 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 7 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> 8 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html:html lang="true"> 12 <head> 13 14 <title>hello.jsp</title> 15 16 </head> 17 18 <body> 19 <html:errors/> 20 <logic:present name="msg" scope="request"> 21 <h2>${msg}</h2> 22 </logic:present> 23 <html:form action="" method="post"> 24 请输入信息:<html:text property="info"></html:text> 25 <html:submit value="显示"></html:submit> 26 </html:form> 27 </body> 28 </html:html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 3 4 <struts-config> 5 <form-beans > 6 <form-bean name="helloForm" type="com.du.struts.form.HelloForm" /> 7 8 </form-beans> 9 10 <global-exceptions /> 11 <global-forwards /> 12 <action-mappings > 13 <action 14 attribute="helloForm" 15 input="/hello.jsp" 16 name="helloForm" 17 path="/hello" 18 scope="request" 19 type="com.du.struts.action.HelloAction" 20 cancellable="true" > 21 <forward name="show" path="/hello.jsp"></forward> 22 </action> 23 24 </action-mappings> 25 26 <message-resources parameter="com.du.struts.ApplicationResources" /> 27 </struts-config>
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 3 <!-- 这是Struts已经添加好的标签库,我们直接使用即可 --> 4 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 5 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 6 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 7 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> 8 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html:html lang="true"> 12 <head> 13 14 <title>hello.jsp</title> 15 16 </head> 17 18 <body> 19 <html:errors/> 20 <logic:present name="msg" scope="request"> 21 <h2>${msg}</h2> 22 </logic:present> 23 <html:form action="" method="post"> 24 请输入信息:<html:text property="info"></html:text> 25 <html:submit value="显示"></html:submit> 26 </html:form> 27 </body> 28 </html:html>
1 /* 2 * Generated by MyEclipse Struts 3 * Template path: templates/java/JavaClass.vtl 4 */ 5 package com.du.struts.form; 6 7 import javax.servlet.http.HttpServletRequest; 8 import org.apache.struts.action.ActionErrors; 9 import org.apache.struts.action.ActionForm; 10 import org.apache.struts.action.ActionMapping; 11 import org.apache.struts.action.ActionMessage; 12 13 /** 14 * MyEclipse Struts 15 * Creation date: 07-20-2017 16 * 17 * XDoclet definition: 18 * @struts.form name="helloForm" 19 */ 20 public class HelloForm extends ActionForm { 21 /* 22 * Generated fields 23 */ 24 25 /** info property */ 26 private String info; 27 28 /* 29 * Generated Methods 30 */ 31 32 /** 33 * Method validate 34 * @param mapping 35 * @param request 36 * @return ActionErrors 37 */ 38 //ActionFrom类用于验证,info属性与表单提交的参数名称一致,并设置了setter和getter操作 39 public ActionErrors validate(ActionMapping mapping, 40 HttpServletRequest request) { 41 ActionErrors errors=new ActionErrors(); 42 if(this.info==null||"".equals(this.info)){//info的输入内容为空 43 //保存错误信息,一个ActionErrors可以包含多个ActionMessage, 44 //ActionMessage类的构造方法中需要传递一个指定错误信息的key,错误信息在ApplicationResource.properties中定义 45 //资源文件不支持中文,会将中文自动转换为Unicode编码 46 errors.add("info",new ActionMessage("error.info")); 47 } 48 return errors; 49 } 50 51 /** 52 * Method reset 53 * @param mapping 54 * @param request 55 */ 56 public void reset(ActionMapping mapping, HttpServletRequest request) { 57 // TODO Auto-generated method stub 58 } 59 60 /** 61 * Returns the info. 62 * @return String 63 */ 64 public String getInfo() { 65 return info; 66 } 67 68 /** 69 * Set the info. 70 * @param info The info to set 71 */ 72 public void setInfo(String info) { 73 this.info = info; 74 } 75 }
1 /* 2 * Generated by MyEclipse Struts 3 * Template path: templates/java/JavaClass.vtl 4 */ 5 package com.du.struts.action; 6 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import org.apache.struts.action.Action; 10 import org.apache.struts.action.ActionForm; 11 import org.apache.struts.action.ActionForward; 12 import org.apache.struts.action.ActionMapping; 13 import com.du.struts.form.HelloForm; 14 15 /** 16 * MyEclipse Struts 17 * Creation date: 07-20-2017 18 * 19 * XDoclet definition: 20 * @struts.action path="/hello" name="helloForm" input="/hello.jsp" scope="request" validate="true" 21 */ 22 public class HelloAction extends Action { 23 /* 24 * Generated Methods 25 */ 26 27 /** 28 * Method execute 29 * @param mapping 30 * @param form 31 * @param request 32 * @param response 33 * @return ActionForward 34 */ 35 public ActionForward execute(ActionMapping mapping, ActionForm form, 36 HttpServletRequest request, HttpServletResponse response) { 37 HelloForm helloForm = (HelloForm) form;// HelloFrom对象 38 String info=helloForm.getInfo();//所有的输入内容从ActionFrom取出 39 request.setAttribute("msg", info);//设置request的属性范围,另外每一个Action都需要一个跳转路径,到配置文件去设置 40 return mapping.findForward("show"); 41 } 42 }
1 error.info=\u8F93\u5165\u4FE1\u606F\u4E0D\u80FD\u4E3A\u7A7A\uFF01
写的过程中所犯的错误:
ActionFrom类里面的错误信息保存New Message 不是Messages啊
java基础学习笔记day01
java基础学习笔记day011.软件开发: 软件:按照特定顺序组织的计算机数据和指令的集合 开发:软件的制作过程 软件开发:借助开发工具和计算机语言制作软件2.java概述: java之父:詹姆斯·高斯林 ... 查看详情
java基础笔记day01
JAVA开发之路---基础篇软件开发软件:一系列按照特定顺序组织的计算机数据和指令的集合常见的软件: 系统软件:windowsdosIOS 应用软件:扫雷等桌面软件什么是开发:制作软... 查看详情
java基础-day01
今日内容介绍1、Java开发环境搭建2、HelloWorld案例3、注释、关键字、标识符4、数据(数据类型、常量) ###01java语言概述*A:java语言概述*a:Java是sun公司开发的一门编程语言,目前被Oracle公司收购,编程语言就是用来编写软件... 查看详情
struts2学习之day01
1.struts21.1前端控制器的配置1.2action类方法的编写要求1.3如何进行页面跳转(这里是转发还是重定向)1.4配置文件加载顺序(struts-default.xml,struts-plugin.xml,struts.xml),有哪些配置文件1.5常量的配置1.6action的编写和访问(3种)本文出... 查看详情
day01-基础内容
day01-基础内容 1.Linux: 1)开源的操作系统、免费的 主要用于服务器端,而Java主要是服务器端开发 2)Linux与Windows目录结构的区别: 2.1)文件系统不同: &n... 查看详情
01day_java基础语法
1、Java开发环境概述JDK:Java开发工具包(JavaDevelopmentKit),包括java编译器、java运行时环境和常用的类库;JRE:Java运行时环境(JavaRuntimeEnvironment)。2、跨平台特性①平台指的是操作系统(Windows,Linux,Mac)②Java程序可以在任意... 查看详情
day01(pyhon基础)
python追求的理念:优美.清晰,简单 python语言类型:解释性的语言机器语言:010101解释型:一条一条的解释成及其语言,运行速度没有编译型的快编译型:把代码一句话编译成机器语言并且保持成二进制文件,运行速度快python优点:1优雅,... 查看详情
day01(java基础)
2021-11-101.JDK,JRE,JVM三者之间的关系,以及JDK、JRE包含的主要结构有哪些?JDK=JRE+Java的开发工具(javac.exe,java.exe,javadoc.exe)JRD=JVM+Java核心类库2.如何配置环境java 查看详情
cgbtn2110-day01总结复习
DAY01复习今天涉及到的笔记:01计算机基础知识与JAVA相关基础知识02JAVA编程环境搭建:安装JDK与配置环境变量、Eclipse03JAVA入门案例之HelloWorld练习与解析1基础知识1.笔记在哪里?CSDN2.代码在哪里?在前言的帖子里有账号... 查看详情
java小白训练营day01-开发环境:helloworld+注释(代码片段)
...,现今开发不是从头从底层去开发,都是在前人基础之上来开发java就直接提供开发平台,我们直接在这个平台之上进行开发平台java提供3个:1)JavaSE,提供很多基础工具类,上万个,封装,给... 查看详情
产品经理基础阶段day01
产品经理基础阶段day01
产品经理基础阶段day01
产品经理基础阶段day01
cgbtn2111-day01总结复习(代码片段)
DAY01复习今天涉及到的笔记:01计算机基础知识与JAVA相关基础知识02JAVA编程环境搭建:安装JDK与配置环境变量、Eclipse03JAVA入门案例之HelloWorld练习与解析1基础知识1.笔记在哪里?CSDN2.代码在哪里?在前言的帖子里有账号... 查看详情
cgbtn2109_day01复习总结(代码片段)
DAY01今天涉及到的笔记:01计算机基础知识与JAVA相关基础知识02JAVA编程环境搭建:安装JDK与配置环境变量、Eclipse03JAVA入门案例之HelloWorld练习与解析1基础知识1.笔记在哪里?CSDN2.代码在哪里?在前言的帖子里有账号密码3... 查看详情
struts2开发基础
Struts2开发基础struts2采用拦截器的机制来处理用户的请求,使得业务逻辑控制器能够与ServletAPI完全脱离开。 1. Hello World!配置web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appid="WebApp_9"version="2.4"xmlns="http://java 查看详情
java语法技术课第1课day01_java基础语法&helloworld(代码片段)
day01_Java基础课程目标1.【了解】Java语言发展史2.【理解】Java语言平台版本3.【理解】Java语言特点4.【理解】JRE与JDK5.【掌握】Java开发环境搭建6.【掌握】第一个Java程序7.【掌握】注释8.【理解】关键字/标识符一.Java语言概述Java语... 查看详情