第一个struts程序的配置过程

第一个struts程序的配置过程

然后输入project-name,比如说“test",点finish,配置web.xml,这里的org.apache.struts.action.ActionServlet就在struts-core-1.3.10.jar中,在后面的步骤中我们要把这些jar包拷贝到WEB-INF/lib中

 <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>

配置server.xml(%CATALINA_HOME%\conf\server.xml),添加一个虚拟路径:

<Context path="/test" docBase="E:\web.workspace\test\WebContent" reloadable="true"/>

然后把struts的所有jar包拷贝到你的WEB-INF/lib中,对于我当前的情况,就是把

F:\_tools\java_libs\struts1.x\struts-1.3.10-all\struts-1.3.10\lib

给拷贝到了

E:\web.workspace\test\WebContent\WEB-INF\lib

然后:

第一个struts程序的配置过程

你再看:现在Web App Libraries中出现了你拷贝到WEB-INF\lib中的jar包

第一个struts程序的配置过程

当然,如果你还有别的需要的jar包用来在eclipse中编译一些java文件,比如servlet-api.jar和jsp-api.jar,那就通过project->properties->Java Build Path->Libraries->Add External JAR来添加,注意,这些jar包放到WEB-INF\lib中是不能帮助eclipse来编译java文件的,struts那些jar包是特例

现在你就写好你的java和各种页面文件,把java文件编译之后,拷贝到WEB-INF/classes里面即可,我这里就是:

From: E:\web.workspace\test\build\classes\struts\action\HelloAction.class
  Copied to: E:\web.workspace\test\WebContent\WEB-INF\classes\struts\action\HelloAction.class
From: E:\web.workspace\test\build\classes\struts\form\HelloForm.class
  Copied to: E:\web.workspace\test\WebContent\WEB-INF\classes\struts\form\HelloForm.class

写好了一个hello.jsp文件,一个HelloAction.java,一个HelloForm.java,1个hello.jsp文件,功能是:从一个text框输入信息,提交给hello.jsp,如果text框中内容为空,则在hello.jsp显示错误提示信息,如果text框中内容不为空,那么就在hello.jsp页面显示text框中输入的内容

HelloAction.java

 package 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 struts.form.HelloForm; public class HelloAction extends Action {
@Override
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"); // 此处返回的是一个映射的路径
}
}

HelloForm.java

 package 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; public class HelloForm extends ActionForm { private String info; 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;
} public void reset(ActionMapping mapping, HttpServletRequest request) {
} public String getInfo() {
return info;
} public void setInfo(String info) {
this.info = info;
}
}

hello.jsp(路径为:%docBase%/hello-struts/hello.jsp)

注意,这里的taglib在理论上来说,需要在web.xml中配置<taglib>节点来指明uri和.tld文件的映射关系,但是,因为在前面的步骤中,我们拷贝到WEB-INF/lib中有一个jar包名为“struts-taglib-1.3.10.jar”,这个jar包中有我们所需的.tld文件,并且,这些tld文件中已经定义好了我们可以使用的URI,因此,在这里我们直接用就行了,如果你不清楚应该用哪个uri,你去jar包里查看一下对应的tld文件的内容就可以找到了(在该jar包的META-INF\tld文件夹中)。

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
<logic:present name="msg" scope="request">
<h2>${msg}</h2>
</logic:present>
<html:form action="/my-first-struts-program/hello.do" method="post">
请输入信息:<html:text property="info"></html:text>
<html:submit value="显示"></html:submit>
</html:form>
</body>
</html:html>

现在,我们需要配置WEB-INF/struts-config.xml,也就是指定我们上面写的Action和ActionForm的配置信息:

<?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="struts.form.HelloForm" />
</form-beans> <global-exceptions />
<global-forwards />
<action-mappings>
<!--
attribute|name: 都是指定此Action要使用的ActionForm的name,跟<form-bean>中的name域一致
input: 当验证出错(ActionErrors不为空)时跳转到的错误处理页
path: 此Action对应的虚拟路径(uri)
scope: 此Action的作用范围,可以是request、session
type: 此Action对应的package、class-name
-->
<action attribute="helloForm" input="/hello-struts/hello.jsp"
name="helloForm" path="/my-first-struts-program/hello" scope="request"
type="struts.action.HelloAction">
<!--
name: 页面的uri
path: 页面的实际路径
-->
<forward name="show" path="/hello-struts/hello.jsp"></forward>
</action>
</action-mappings> <message-resources parameter="struts.ApplicationResources" />
</struts-config>

这里还需要说的就是,hello.jsp中action="/test/hello.do",之所以这样用,是因为<action>配置的path为/test/hello,而web.xml中配置的<url-pattern>为*.do

另一方面,<message-resouces>的parameter指出了我们的ApplicationResources.properties文件的位置:E:\web.workspace\test\WebContent\WEB-INF\classes\struts\ApplicationResources.properties

其内容如下,当提交的信息为空的时候,显示"error.info"指定的信息:

# 输入的信息不能为空!
error.info = \u8f93\u5165\u7684\u4fe1\u606f\u4e0d\u80fd\u4e3a\u7a7a\uff01

测试1:输入的信息为空,直接提交

第一个struts程序的配置过程

测试2:输入一些信息,提交,用filter给request和response做一下setCharacterEncoding("UTF-8"),解决编码问题

第一个struts程序的配置过程

第一个struts程序的配置过程

filter代码如下:

package org.inf.filters;

import java.io.IOException;
import java.nio.charset.Charset; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean enable = false;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (this.enable) {
String encoding = this.selectEncoding(request);
if (encoding != null && !encoding.equals("")) {
System.out.println("~~" + this + ": request :" + encoding);
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
if (this.enable) {
String encoding = this.selectEncoding(request);
if (encoding != null && !encoding.equals("")) {
System.out.println("~~" + this + ": response :" + encoding);
response.setCharacterEncoding(encoding);
}
}
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
if (!Charset.isSupported(encoding)) {
encoding = null;
}
String enableString = filterConfig.getInitParameter("enable");
if (enableString.equalsIgnoreCase("true")) {
this.enable = true;
} else {
this.enable = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}

web.xml配置:

    <filter>
<filter-name>encoding</filter-name>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
<filter-class>org.inf.filters.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
上一篇:ASP.NET请求管道、应用程序生命周期、整体运行机制


下一篇:Qt Charts_Audio实践