Struts框架的使用
1.编写Action类
2.Struts配置
1) 配置Struts核心过滤器
2) 配置Action映射
1. 导包
a) 注意:这个包是在Struts2与Spring整合的时候用的,单用Struts2的时候一定不能导这个包。
2. 编写Action类
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
private static final long serialVersionUID = -466504590042207559L;
private String name;
private String password;
public DemoAction() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() {
return SUCCESS;
}
}
3. 在web.xml部署描述符中进行如下配置:
<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>
4. Action组件必须在struts.xml文件中进行配置后才能正常使用。Struts.xml文件必须位于应用程序的WEB-INF/classes中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" namespace="/demo"extends="struts-default">
<action name="DemoAction" class="cn.edu.bucea.action.DemoAction">
<result name="success">/index.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
5. 编写Demo.jsp
<body>
<p>This is Demo.</p>
<s:form action="Demo" method="post"namespace="/demo" theme="simple">
姓名:<s:textfield name="name" /><s:fielderror name="name"/><br /><br />
密码:<s:textfield name="password" /><s:fielderror name="password"/>
<s:submit value="提交"/>
</s:form>
</body>
注:以上是Struts2的应用
==============================================================================
下一篇:SSH框架搭建过程---之Struts整合Spring