action类中的变量a的值如果要传到页面上,jsp页面中要有与action中定义的a变量相同名字的变量;同时jsp页面中要先引入struts的页面标签;<%@ taglib prefix="s" uri="/struts-tags"%> 这样就可以在页面中使用jsp标签进行页面与action类(继承了ActionSupport类)的参数交互,即可以实现把页面的值传到后台或者把后台的值传到页面。
struts.xml文件:
<struts>
<package name="default" extends="struts-default">
<action name="StrutsDemo" class="sncy.Struts2Demo1">
<result>/Servlet.jsp</result>
</action>
</package>
</struts>
打开web.xml文件,将其修改为以下代码:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Struts 2.0 Hello World</display-name>//此标签无实际意义
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>