java struts 中的 web.xml 以及如何使用 struts-config.xml 对其进行配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10004912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
web.xml in struts and how it is configured with struts-config.xml
提问by saplingPro
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
The above code was automatically generated by my IDE when I choose struts framework for my project. I don't see any servlet named action. Please explain what this xml means?
以上代码是我的项目选择struts框架时IDE自动生成的。我没有看到任何名为action 的servlet 。请解释一下这个xml是什么意思?
EDIT :
编辑 :
I read that ActionServlet has been configured with the struts-config.xml file. How it is configured ?
我读到 ActionServlet 已经配置了 struts-config.xml 文件。它是如何配置的?
<struts-config>
<form-beans>
<form-bean name="HelloWorldActionForm"
type="com.vaannila.HelloWorldActionForm"/>
<action-mappings>
<action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld" scope="session" type="com.vaannila.HelloWorldAction">
<forward name="success" path="/helloWorld.jsp" />
</action>
</action-mappings>
回答by óscar López
The configuration file shown says this:
显示的配置文件是这样说的:
- All URLs which end in
.do
will be processed by a servlet namedaction
- The servlet named
action
corresponds to the classorg.apache.struts.action.ActionServlet
- 所有以 结尾的 URL
.do
将被一个名为action
- 命名的 servlet
action
对应于类org.apache.struts.action.ActionServlet
回答by Adi
Here is how Struts works:
下面是 Struts 的工作原理:
Struts has a FrontController. This means all request are going through this controller. This is the org.apache.struts.action.ActionServlet. This class is using the struts-configto pass the request to an other class.
Struts 有一个 FrontController。这意味着所有请求都通过这个控制器。这是 org.apache.struts.action.ActionServlet。这个类使用struts-config将请求传递给另一个类。
You have specified that everytime the URL: /HelloWorldis request the ActionServlet is passing the request to the class com.vaannila.HelloWorldActionWhen your class is returning successthe ActionServlet will display the jsp: helloWorld.jsp
您已指定每次 URL:/HelloWorld请求 ActionServlet 将请求传递给类com.vaannila.HelloWorldAction当您的类返回成功时,ActionServlet 将显示 jsp:helloWorld.jsp
回答by SivaStack
By default ActionServlet is configured to /WEB-INF/struts-config.xml file under your web application project directory.
默认情况下,ActionServlet 被配置为您的 web 应用程序项目目录下的 /WEB-INF/struts-config.xml 文件。
for example: if your project name is StrutsPractice then you can find the default configuration file in the path /StrutsPractice/src/main/webapp/WEB-INF/struts-config.xml
例如:如果您的项目名称是 StrutsPractice 那么您可以在路径 /StrutsPractice/src/main/webapp/WEB-INF/struts-config.xml 中找到默认配置文件
To explicitly configure the ActionServlet or you want to configure it to a config file in a different path then you can configure it like below in web.xml
要显式配置 ActionServlet 或者您想将其配置为不同路径中的配置文件,那么您可以在 web.xml 中进行如下配置
<servlet>
<servlet-name>strutspractice</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>strutspractice</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>