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 void validate() {
System.out.println("validate:" + name + "\t" + password);
if(name == null || "".equals(name)) {
addFieldError("name", "用户名字段为空。");
}
if(password == null || "".equals(password)) {
addFieldError("password", "密码字段为空。");
}
}
@Override
public String execute() {
System.out.println("execute:" + name + "\t" + password);
Title title = new Title();
title.setTitle("起亚新K3开启汽车智能新时代");
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
//把title存储到请求作用域
request.setAttribute("title", title);
return SUCCESS;
}
}
在web.xml部署描述符中进行如下配置:
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
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="com.justcrazy.www.action.DemoAction">
<result name="success">/index.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
- 编写Demo.jsp
This is Demo.
姓名:
密码:
注:以上是Struts2的应用
下一篇:SSH框架搭建过程—之Struts整合Spring