首页 > 代码库 > struts的学习

struts的学习

在开发工具上进行struts的配置

1)新建项目,为项目添加Struts开发支持

2)为项目添加Struts开发支持,在项目点击右键,myeclipse->Add Struts Capabilities。选择支持的struts版本

3)在项目下的WebRoot下新建一个jsp文件。在Template to use下选在2中选择的struts版本

配置hello.jsp

<%@ page language="java" pageEncoding="GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %><%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %><html:html lang="true">  <head>    <html:base />        <title>hello.jsp</title>  </head>    <body>    <html:form action = "" method = "post">        请输入信息:<html:text property = "info"></html:text>        <html:submit value = "http://www.mamicode.com/显示"></html:submit>    </html:form>  </body></html:html>

在struts之中,所有的处理类都是Action,都要绑定ActionForm

1)点击空白处,选着custom........->shortcuts->MyEclipse->Web-Struts->Struts 1.3->Struts 1.3 Form,Action......

2)在lcp.struts->new->Struts 1.3Form,Action,Jsp

这里的info属性和表单对应。

点击next,把Input Sroce改成/hello.jsp

WebRoot/lib/struts-config.xml里的action配置path表示的是提交路径。input表示的是错误信息的显示页面

lop.struts.form/HelloForm.java里面的validate()方法,主要是晚场具体的验证操作

ActionErrors是一组信息,每一个错误信息依靠ActionMessage保存。

struts之中,所有的错误信息都保存在资源文件里的

定义资源文件:

 ApplicationResources.properties:error.info = 输出错误信息。此时不能保存,不支持中文。通过jdk转码。找到自己安装的jdk路径:C:\Program Files (x86)\Java\jdk1.8.0_11\bin里的native2ascii.exe。输入想要的中文:

输入的消息不能为空
\u8f93\u5165\u7684\u6d88\u606f\u4e0d\u80fd\u4e3a\u7a7a

public ActionErrors validate(ActionMapping mapping,			HttpServletRequest request) {		ActionErrors errors = new ActionErrors();		if(this.info == null || "".equals(this.info)){//info的输入信息为空			//保存错误信息			errors.add(info,new ActionMessage("errors.info"));		}		return null;	}

  此方法若返回的是null,或者ActionErrors对象里美欧任何的内容,则表示没有任何错误

 

 在Action之中,如果想要完成跳转功能,需要配置。struts-config.xml文件中完成。。每一个action配置一个跳转路径。以后想要修改跳转路径,直接修改配置文件即可。

 

ApplicationResources.properties文件配置中文编码集,转码问题。

# Resources for parameter ‘lop.struts.ApplicationResources‘# Project hherror.info =\u60A8\u8F93\u5165\u7684\u8FC7\u957F

lop.struts.action/HelloAction.java

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package lop.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import lop.struts.form.HelloForm;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;/**  * MyEclipse Struts * Creation date: 10-16-2014 *  * XDoclet definition: * @struts.action path="/hello" name="helloForm" input="/hello.jsp" scope="request" validate="true" */public class HelloAction extends Action {	/*	 * Generated Methods	 */	/** 	 * Method execute	 * @param mapping	 * @param form	 * @param request	 * @param response	 * @return ActionForward	 */	public ActionForward execute(ActionMapping mapping, ActionForm form,			HttpServletRequest request, HttpServletResponse response) {		HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method stub		String info = helloForm.getInfo();//所有的内容从ActionForm取出		if(info.length() > 15){//数据过长			ActionMessages errors = new ActionMessages();			errors.add("info", new ActionMessage("msg.info"));			//super.saveErrors(request, errors);//保存错误信息			super.saveMessages(request, errors);//保存错误信息			return mapping.getInputForward();//取得input配置的路径		}else{			request.setAttribute("msg", info);//将信息设置在request范围之中		}		return mapping.findForward("show");//返回一个映射路径	}}

 lop.struts.form/HelloForm.java

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package lop.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;/**  * MyEclipse Struts * Creation date: 10-16-2014 *  * XDoclet definition: * @struts.form name="helloForm" */public class HelloForm extends ActionForm {	/*	 * Generated fields	 */	/** info property */	private String info;	/*	 * Generated Methods	 */	/** 	 * Method validate	 * @param mapping	 * @param request	 * @return ActionErrors	 */	public ActionErrors validate(ActionMapping mapping,			HttpServletRequest request) {		ActionErrors errors = new ActionErrors();		if(this.info == null || "".equals(this.info)){//info的输入信息为空			//保存错误信息			errors.add(info,new ActionMessage("error.info"));		}		return errors;	}	/** 	 * Method reset	 * @param mapping	 * @param request	 */	public void reset(ActionMapping mapping, HttpServletRequest request) {		// TODO Auto-generated method stub	}	/** 	 * Returns the info.	 * @return String	 */	public String getInfo() {		return info;	}	/** 	 * Set the info.	 * @param info The info to set	 */	public void setInfo(String info) {		this.info = info;	}}

  lib/ruts-config.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config>  <form-beans >    <form-bean name="helloForm" type="lop.struts.form.HelloForm" />  </form-beans>  <global-exceptions />  <global-forwards />  <action-mappings >    <action      attribute="helloForm"      input="/hello.jsp"      name="helloForm"      path="/hello"      scope="request"      type="lop.struts.action.HelloAction">      <forward name = "show" path = "/hello.jsp"></forward>     </action>  </action-mappings>  <message-resources parameter="lop.struts.ApplicationResources" /></struts-config>

 hello.jsp

<%@ page language="java" pageEncoding="GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %><%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %><html:html lang="true">  <head>    <html:base />        <title>hello.jsp</title>  </head>    <body>  	<html:messages id = "info" message = "true">  		$(info)  	</html:messages> 	<html:errors/> 	<logic:present name = "msg" scope = "request">  		<H2>${msg}</H2>  	</logic:present>    <html:form action = "hello.do" method = "post">    	请输入信息:<html:text property = "info"></html:text>    	<html:submit value = "http://www.mamicode.com/显示"></html:submit>    </html:form>  </body></html:html>

  

 

  

 

struts的学习