Struts2 配置问题!在线等

simplexc 2013-07-12 10:37:43
各位大牛们,初学struts2 简单配置了一下,出现了问题,直接上代码!望各位指点迷津!
Java类
package com.test;

import com.opensymphony.xwork2.ActionSupport;

public class QueryAction extends ActionSupport{
public String execute(){
System.out.println("fdasdfsdfsdfdsf");
return "success";
}
}

struts.xml 直接放在了src下,tomcat启动时classes下有这个文件
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 指定Struts2配置文件dtd信息 -->
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
<!-- Struts 2的Action必须放在指定的包空间下定义 -->
<package name="test" extends="struts-default">
<action name="query" class="com.test.QueryAction">
<result name="error">/error.jsp</result>
<result name="success">/query.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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 定义Struts 2的FilterDispatcher的Filter -->
<filter>
<!-- 定义核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心Filter的实现类 -->
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!--<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>-->
</filter>
<!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>


jsp
<body>
<form action="query.do" method="post">
<input type="submit" value="提交">
</form>
</body>
...全文
249 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
simplexc 2013-07-15
  • 打赏
  • 举报
回复
谢谢楼上各位竭力帮忙,我的问题已经解决了!主要是路径配置的问题,以下是源码 jsp: <body> <form action="HelloAction.action" method="post"> 姓名:<input type="text" /> <br/> 性别:<input type="text" /> <br/> <input type="submit" value="提交"/> </form> </body> Action public class HelloAction { public String execute() { System.out.println("safasdfasd"); return "YES"; } } Struts.xml <struts> <package name="default" namespace="/" extends="struts-default"> <action name="HelloAction" class="com.test.action.HelloAction" method="execute"> <result name="YES">/Hello.jsp</result> </action> </package> </struts> web.xml不要动! 谢谢splendid_java 提供的实例
bobo928843007 2013-07-13
  • 打赏
  • 举报
回复
你用选择*.action,还能用*.do做路径吗,改成*.action试试吧。我没学过structs
鬼门关 2013-07-13
  • 打赏
  • 举报
回复
jsp中把action=“query.do”改个名字试试,如myquery.do,web.xml也改为myquery
Mr-稻帅 2013-07-13
  • 打赏
  • 举报
回复
尼玛 看不下去了,路径缺少package的名称的 你这样提交 struts不知道到哪个package上的 <form action="test/query.do" method="post">
  • 打赏
  • 举报
回复
引用 12 楼 simplexc 的回复:
[quote=引用 9 楼 hjw506848887 的回复:] 你的action中出问题了。。。。 <action name="query" class="com.test.QueryAction"> <result name="error">/error.jsp</result> <result name="success">/query.jsp</result> </action> 这个位置和你jsp中的<form action="query.do" method="post">不一致。
请问这个地方需要怎么配置?在struts.xml中 action name 应该是不需要加.do的吧,jsp页面添加.do是让拦截器过滤请求的吧、不知道理解的对不对、[/quote]其实后缀不是.do而是.action,如果改为.action就没错了,当然你的web.xml中的url-pattern也要改,一般都直接写成 <url-pattern>/*</url-pattern>。并且.action也是可有可无的,不写也行,反正我很少加这个。。。。。。
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 13 楼 u010241813 的回复:
<?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">


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>com.accp.filter.EncodeFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

  • 打赏
  • 举报
回复
struts1中拦截请求是用xxxaction.do?method=xxx来实现的 struts2中直接用xxxaction来提交,如果action中有多个方法的话, xxxaction!add(del,update).action 来提交 不明白文档上干嘛要把struts2的拦截改成*.do
  • 打赏
  • 举报
回复
<?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">

 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext-*.xml</param-value>
 </context-param>
 <filter>
  <filter-name>encodeFilter</filter-name>
  <filter-class>com.accp.filter.EncodeFilter</filter-class>
 </filter>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>encodeFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>dwr</servlet-name>
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>dwr</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>login.html</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 9 楼 hjw506848887 的回复:
你的action中出问题了。。。。 <action name="query" class="com.test.QueryAction"> <result name="error">/error.jsp</result> <result name="success">/query.jsp</result> </action> 这个位置和你jsp中的<form action="query.do" method="post">不一致。
请问这个地方需要怎么配置?在struts.xml中 action name 应该是不需要加.do的吧,jsp页面添加.do是让拦截器过滤请求的吧、不知道理解的对不对、
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 10 楼 u010241813 的回复:
struts1和sturts2混淆了
我看着文档做的,struts2是这样子配置的吧
  • 打赏
  • 举报
回复
struts1和sturts2混淆了
  • 打赏
  • 举报
回复
你的action中出问题了。。。。 <action name="query" class="com.test.QueryAction"> <result name="error">/error.jsp</result> <result name="success">/query.jsp</result> </action> 这个位置和你jsp中的<form action="query.do" method="post">不一致。
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 7 楼 rencht 的回复:
后台有执行到System.out.println("fdasdfsdfsdfdsf");吗?
没有呢
rencht 2013-07-12
  • 打赏
  • 举报
回复
后台有执行到System.out.println("fdasdfsdfsdfdsf");吗?
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 2 楼 wnn654321 的回复:
出了什么问题呢。。。
点击jsp上的提交,出现404……
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 1 楼 hjw506848887 的回复:
异常信息是什么呀,不然没法看的。。。。。。
点击jsp上的提交,出现404、郁闷呀、
simplexc 2013-07-12
  • 打赏
  • 举报
回复
引用 3 楼 hjw506848887 的回复:
<url-pattern>*.do</url-pattern>你的web.xml中的这是干嘛的???还有这个 <form action="query.do" method="post">这是struts2不是struts1啊,改成<url-pattern>/*</url-pattern>,把query.do改为query,你再试试。。。。。。
我配置的就是struts2 点击jsp上的提交,出现404、郁闷呀、
  • 打赏
  • 举报
回复
<url-pattern>*.do</url-pattern>你的web.xml中的这是干嘛的???还有这个 <form action="query.do" method="post">这是struts2不是struts1啊,改成<url-pattern>/*</url-pattern>,把query.do改为query,你再试试。。。。。。
  • 打赏
  • 举报
回复
出了什么问题呢。。。
  • 打赏
  • 举报
回复
异常信息是什么呀,不然没法看的。。。。。。
加载更多回复(3)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧