码迷,mamicode.com
首页 > 其他好文 > 详细

第一个Struts程序

时间:2020-05-13 23:01:16      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:rate   path   new   pack   ati   gen   text   ips   content   

1.配置好TOMCAT服务器,设置/mldn为访问路径,F:/mldnweb为真实路径;

2.配置Struts:

  拷贝文件:

  (1)将Struts开发包中lib目录下的全部*.jar包复制到%TOMCAT_HOME%\lib目录中,即F:\mldnweb\WEB-INF\lib目录中(或D:\ProgramFiles\Tomcat 8.5\lib目录中)

  (2)将Struts开发包中lib目录下的struts-taglib-1.3.10.jar文件解压,将解压后\struts-taglib-1.3.10\META-INF\tld目录下的所有*.tld文件拷贝至F:\mldnweb\WEB-INF目录下(该目录位置与后续F:\mldnweb\WEB-INF目录中web-xml中配置的tld文件路径位置对应即可),包括struts-bean.tld、struts-html.tld及struts-logic.tld等

  配置文件:

  (1)F:\mldnweb\WEB-INF目录中web-xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>http://struts.apache.org/tags-bean</taglib-uri>
<taglib-location>/WEB-INF/strus-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://struts.apache.org/tags-html</taglib-uri>
<taglib-location>/WEB-INF/strus-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://struts.apache.org/tags-logic</taglib-uri>
<taglib-location>/WEB-INF/strus-logic.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>

  (2)F:\mldnweb\WEB-INF目录中struts-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="org.lxh.struts.form.HelloForm" />
</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute="helloForm" input="/hello.jsp"
name="helloForm" path="/hello" scope="request"
type="org.lxh.struts.action.HelloAction">
<forward name="show" path="/hello.jsp"></forward>
</action>
</action-mappings>

<message-resources parameter="org.lxh.struts.ApplicationResources" />
</struts-config>

3.编写代码:

  (1)F:\mldnweb\目录中的hello.jsp文件:

     <%@ page contentType="text/html" 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"%>
<html:html lang="true">
<head>
<title>hello.jsp</title>
</head>
<body>
<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="显示"></html:submit>
</html:form>
</body>
</html:html>

  (2)F:\mldnweb\WEB-INF\classes目录中的HelloAction.java文件:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.lxh.struts.action;

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;
import org.lxh.struts.form.HelloForm;

/**
* MyEclipse Struts Creation date: 07-09-2009
*
* 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
String info = helloForm.getInfo(); // 所有的输入内容从ActionForm取出
request.setAttribute("msg", info); // 将信息设置在request范围之中
return mapping.findForward("show"); // 此处返回的是一个映射的路径
}
}

  (3)F:\mldnweb\WEB-INF\classes目录中的HelloForm.java文件:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.lxh.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: 07-09-2009
*
* 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;
}
}

第一个Struts程序

标签:rate   path   new   pack   ati   gen   text   ips   content   

原文地址:https://www.cnblogs.com/huhewei/p/12885484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!