我是个初学者,刚刚接触了struts2,然而我的第一个struts2的例子弄了两三天也没弄明白,组后找的朋友帮我看了5分钟左右就调出来了,有些错误很简单,都是我们不经意间犯下的。
下面说说我是怎么解决我遇到的困难的。
第一个地方
在导入struts2所必须的jar包时,一定不要在tomcat中和你的工程目录下都放入这些包,这会引起冲突,我在NetBeans里面运行时就提示我什么build.xml尚未部署。
这些包都是:struts2-core-2.1.6.jar ,freeer-2.3.13.jar ,commons-logging-1.0.4.jar ,ognl-2.6.11.jar,xwork-2.1.2.jar ,commons-fileupload-1.2.1.jar这些包只是在只简单建立struts2所需要的包,应用别的架构还要在添加! 第二个地方
由于现在struts2的版本也并不是都一样,在web.xml中的配置也不尽相同,目前以struts2.1.8配置为准,
网上大部分人给出的web.xml配置都像下面的:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<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> 而2.1.8有所不同,它的filter-class中的内容必须为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
这一点我也是才知道。
我的web.xml为:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我的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>
<package name="newpackage" extends="struts-default" >
<action name="hello"
class="newpackage.HelloAction">
<result name="success">/b.jsp</result>
</action>
</package>
</struts> 第三点
主要是用NetBeans的同僚们,NetBeans不像MyEclipse的用户那么多,遇到错误解决起来很棘手,
我的错误是struts.xml的位置放错了,而我一直以为我放的位置是正确的。
struts.xml一定要放到源包的跟目录下,你可以直接右键点击“源包”,建立xml文件,之后它会给你把你所创建的struts.xml文件放在<缺省包>中,就是这一点一直困扰我到最后,很愁人,但有很简单的一不,希望和我一样的初学者不要再范和我相同的错误,前车之鉴啊!
剩下的就没什么了,主要是千万不要有拼写错误之类的。还有什么错误,网上有解决的大概比较多,只是像我这样的错误还是比较少见的,所以贴出来给大家看看。顺便把我写的第一个struts2的例子给贴出来,大家可以粘贴出去测试一下。
a.jsp
<html>
<head>
<title>Welcome</title></head>
<body>
<h3>Commands</h3>
<a href="hello.action">testMyAction</a></body>
</html>
该页面只有一个链接,可以提交请求到相应的action类进行处理,也就是HelloAction.java
HelloAction.java/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package newpackage;
/**
*
* @author Administrator
*/
public class HelloAction {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private String msg;
public String execute()
throws Exception {
return SUCCESS;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
action处理给出一个结果,通过这一结果跳转到b.jsp
b.jsp<%--
Document : b
Created on : 2010-5-10, 13:00:45
Author : Administrator
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
this is page b!
</body>
</html>
程序十分简单,只是为了测试struts2用的,大家可以在其中加点内容可以传递一些你输入的内容之类的完全可以。
祝大家学习愉快~~~~~~~
下面说说我是怎么解决我遇到的困难的。
第一个地方
在导入struts2所必须的jar包时,一定不要在tomcat中和你的工程目录下都放入这些包,这会引起冲突,我在NetBeans里面运行时就提示我什么build.xml尚未部署。
这些包都是:struts2-core-2.1.6.jar ,freeer-2.3.13.jar ,commons-logging-1.0.4.jar ,ognl-2.6.11.jar,xwork-2.1.2.jar ,commons-fileupload-1.2.1.jar这些包只是在只简单建立struts2所需要的包,应用别的架构还要在添加! 第二个地方
由于现在struts2的版本也并不是都一样,在web.xml中的配置也不尽相同,目前以struts2.1.8配置为准,
网上大部分人给出的web.xml配置都像下面的:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<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> 而2.1.8有所不同,它的filter-class中的内容必须为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
这一点我也是才知道。
我的web.xml为:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我的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>
<package name="newpackage" extends="struts-default" >
<action name="hello"
class="newpackage.HelloAction">
<result name="success">/b.jsp</result>
</action>
</package>
</struts> 第三点
主要是用NetBeans的同僚们,NetBeans不像MyEclipse的用户那么多,遇到错误解决起来很棘手,
我的错误是struts.xml的位置放错了,而我一直以为我放的位置是正确的。
struts.xml一定要放到源包的跟目录下,你可以直接右键点击“源包”,建立xml文件,之后它会给你把你所创建的struts.xml文件放在<缺省包>中,就是这一点一直困扰我到最后,很愁人,但有很简单的一不,希望和我一样的初学者不要再范和我相同的错误,前车之鉴啊!
剩下的就没什么了,主要是千万不要有拼写错误之类的。还有什么错误,网上有解决的大概比较多,只是像我这样的错误还是比较少见的,所以贴出来给大家看看。顺便把我写的第一个struts2的例子给贴出来,大家可以粘贴出去测试一下。
a.jsp
<html>
<head>
<title>Welcome</title></head>
<body>
<h3>Commands</h3>
<a href="hello.action">testMyAction</a></body>
</html>
该页面只有一个链接,可以提交请求到相应的action类进行处理,也就是HelloAction.java
HelloAction.java/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package newpackage;
/**
*
* @author Administrator
*/
public class HelloAction {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private String msg;
public String execute()
throws Exception {
return SUCCESS;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
action处理给出一个结果,通过这一结果跳转到b.jsp
b.jsp<%--
Document : b
Created on : 2010-5-10, 13:00:45
Author : Administrator
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
this is page b!
</body>
</html>
程序十分简单,只是为了测试struts2用的,大家可以在其中加点内容可以传递一些你输入的内容之类的完全可以。
祝大家学习愉快~~~~~~~
解决方案 »
- 急!JavaBean的问题?
- 我这个项目dao层怎么设计
- java求解!
- 急求....在页面生成甘特图....与微软的project相似
- 如何使用jsf向数据库中取值?
- ENCTYPE="multipart/form-data"(文件上传问题! 请高手帮忙!)
- 动态实现二级下拉框时的一个奇怪的bug
- struts中的validate是如何写的,我怎么配置不上来了?
- 数据库是显示1,页面上是显示女,是2,页面显示男,要怎么表示呢!
- 读取得文件内容怎么是乱码?
- struts2的第一次,没有我想要的结果!!突然有种被骗的感觉!!无法运行struts2文件,得到解决还可以再加分!!
- 谁有网上书店的后台操作图片呢 有的进来下·~·
第二个问题我也碰到过