我刚学struts2,现在自己写了简单的helloworld程序,但运行时出现了错误,貌似是struts.xml配置问题,忘各位帮忙看看
我的环境是:eclipse3.2 tomcat5.5 jdk1.5
项目名为struts2,目录如下:
src-base-HelloWorld.java
src-struts.xml
WebContent-index.html、SayHello.jsp、HelloWorld.jsp程序如下:
web.xml:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml:<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="base" extends="struts-default">
<action name="HelloWorld" class="base.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
HelloWorld.java:package base;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport { public HelloWorld() {
// TODO Auto-generated constructor stub
} private String username; private String password; public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String execute() {
username = "Hello," + username;
System.out.println(username);
return SUCCESS;
}}
SayHello.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>say hello</title>
</head>
<body>
<s:form action="HelloWorld" method="post">
Say Hello to :<s:textfield name="username" />
<s:submit />
</s:form>
</body>
</html>
HelloWorld.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hello</title>
</head>
<body>
hello,
<s:property value="username"/>
</body>
</html>
启动tomcat后,访问地址为:http://localhost:88/struts2/SayHello.jsp
在文本框中输入字符后,点击submit按钮,就出现了404错误,而且HelloWorld.java中execute()方法,未执行输出,所以我怀疑是不是我的struts.xml配置有问题,可是检查了感觉没错误,请各位帮帮忙,看看
我的环境是:eclipse3.2 tomcat5.5 jdk1.5
项目名为struts2,目录如下:
src-base-HelloWorld.java
src-struts.xml
WebContent-index.html、SayHello.jsp、HelloWorld.jsp程序如下:
web.xml:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml:<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="base" extends="struts-default">
<action name="HelloWorld" class="base.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
HelloWorld.java:package base;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport { public HelloWorld() {
// TODO Auto-generated constructor stub
} private String username; private String password; public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String execute() {
username = "Hello," + username;
System.out.println(username);
return SUCCESS;
}}
SayHello.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>say hello</title>
</head>
<body>
<s:form action="HelloWorld" method="post">
Say Hello to :<s:textfield name="username" />
<s:submit />
</s:form>
</body>
</html>
HelloWorld.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hello</title>
</head>
<body>
hello,
<s:property value="username"/>
</body>
</html>
启动tomcat后,访问地址为:http://localhost:88/struts2/SayHello.jsp
在文本框中输入字符后,点击submit按钮,就出现了404错误,而且HelloWorld.java中execute()方法,未执行输出,所以我怀疑是不是我的struts.xml配置有问题,可是检查了感觉没错误,请各位帮帮忙,看看
WebContent文件夹下WEB-INF\lib下,已经加了
commons-logging-1.0.4.jar
freeer-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
xwork-2.0.7.jar
<s:form action="HelloWorld.do" method="post">
commons-io-1.3.2.jar
把这俩包加上再试试,你看的视频估计用的是早期版本的struts2吧,后来版本升级了必须加上这俩包
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\jdk1.5.0\bin;.;C:\WINDOWS\system32;C:\WINDOWS;.;C:\j2sdk1.4.2_14\bin;F:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\j2sdk1.4.2_14\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\java\jdk1.6.0_07\bin;D:\setup\IBM\SQLLIB\BIN;D:\setup\IBM\SQLLIB\FUNCTION;D:\setup\IBM\SQLLIB\SAMPLES\REPL;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;D:\setup\MySQL\MySQL Server 5.0\bin;D:\apache-ant-1.7.1\bin;C:\Program Files\Subversion\bin;C:\Program Files\TortoiseSVN\bin
2009-12-22 11:14:09 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-88
2009-12-22 11:14:09 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2009-12-22 11:14:09 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 969 ms
2009-12-22 11:14:09 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2009-12-22 11:14:09 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.28
2009-12-22 11:14:09 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2009-12-22 11:14:10 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider register
信息: Parsing configuration file [struts-default.xml]
2009-12-22 11:14:10 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider register
信息: Parsing configuration file [struts-plugin.xml]
2009-12-22 11:14:10 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider register
信息: Parsing configuration file [struts.xml]
2009-12-22 11:14:10 org.apache.struts2.config.Settings getLocale
警告: Settings: Could not parse struts.locale setting, substituting default VM locale
2009-12-22 11:14:11 com.opensymphony.xwork2.util.ObjectTypeDeterminerFactory <clinit>
信息: Setting DefaultObjectTypeDeterminer as default ...
2009-12-22 11:14:12 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-88
2009-12-22 11:14:12 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2009-12-22 11:14:12 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2009-12-22 11:14:12 org.apache.catalina.startup.Catalina start
信息: Server startup in 2734 ms
还有struts.xml 和 web.xml 的头部不知道对不对
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
应该是org.apache.struts2.dispatcher.StrutsPrepareAndExcuteFilter啊
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.StrutsPrepareAndExcuteFilter
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:207)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:302)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:78)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3666)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4258)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
2009-12-22 11:23:18 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2009-12-22 11:23:18 org.apache.catalina.core.StandardContext start
严重: Context [/struts2] startup failed due to previous errors
2009-12-22 11:23:18 org.apache.coyote.http11.Http11AprProtocol start
<include file="struts-default.xml" />
<package name="base" extends="struts-default" namespace="/struts2">
<action name="HelloWorld" class="base.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
我用的是
struts2-core-2.1.6.jar
xwork-2.1.2.jarstruts 过滤器用的是:
<filter>
<filter-name>struts2Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
对于什么版本用什么filter没有仔细研究过。
希望对你有帮助。
System.out.println(username);
return SUCCESS;那个success的路径是哪个啊
<include file="struts-default.xml" />
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="struts2" namespace ="/mystruts"
extends ="struts-default" >
< action name ="sum" class ="action.FirstAction" >
< result name ="positive" > /positive.jsp </ result >
< result name ="negative" > /negative.jsp </ result >
</ action >
</ package >
</ struts >
没用过2,你的package比这个例子少了东西,没关系吗?
还有一个name="success"还有你第一个页面和第二个页面是在同一目录下吗,是的话就/HelloWorld.jsp就不要加/了
<projectDescription>
<name>struts2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
</natures>
</projectDescription>
我重新配置了一下
struts.xml:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="ph" />
<constant name="struts.multipart.maxSize" value="1000000000"/>
<constant name="struts.multipart.saveDir" value="/upload"></constant>
<package name="shangzhou" extends="struts-default">
<action name="HelloWorld" class="com.shangzhou.action.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>web.xml:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<filter>
<filter-name>struts2</filter-name><!-- 命名Struts 2.0核心类 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <!-- 指定Struts 2.0核心类 -->
</filter>
<filter-mapping><!-- 配置核心类处理的请求 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern><!-- 指定处理用户所有请求 -->
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>struts2</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>在webinfo目录下我加了一个struts-tags.tld,因此要那样配置
SayHello.jsp:<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="struts2"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>say hello</title>
</head>
<body>
<s:form action="HelloWorld" method="post">
Say Hello to :<s:textfield name="username" />
<s:submit />
</s:form>
</body>
</html>HelloWorld.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="struts2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hello</title>
</head>
<body>
hello,
<s:property value="username"/>
</body>
</html>java类我没有做任何修改
完全可以运行成功
我的jar包的版本如下:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freeer-2.3.8.jar
ognl-2.6.11.jar
standard.jar
struts2-core-2.0.10.jar
xwork-2.0.4.jar
或者加我的QQ 帮你看看的
chinese wholesalers
uggpassword = password;
} public String getUsername() {
return username;
TOMCAT重新用WAR部署后又正常了,之前使用server.xml部署的就出错
感觉很奇怪,程序应该不受TOMCAT部署方式的影响