作者:910621rh_270 | 来源:互联网 | 2023-10-12 11:52
在表单action值里指定所调用的action中的哪个方法而不是借助配置文件action节点的method属性1UserAction类packageorg.action;
在表单action值里指定所调用的action中的哪个方法而不是借助配置文件action节点的method属性
1 UserAction类
package org.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements
ModelDriven {
//这种实现ModelDriven的模型驱动需要的模型要实例化
private User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add() {
return "add";
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "minus";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
2model类
package org.model;
public class User {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3struts.xml
/success.jsp
/error.jsp
4首页
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
注:action&#20540;可以改成getuser就会调用默认方法excute
用户名:
密码:
5 转发页面error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
username:
password:
6转发页面 success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
用户名:
密码:
7运行截图;
(1)默认调用excute方法转发到error.jsp
2调用add方法