struts2 action怎么写
发布网友
发布时间:2022-10-15 15:39
我来回答
共1个回答
热心网友
时间:2023-10-09 16:38
这个还是用的servlet中的request 和response方法。没体现struts2的优势;
action:
import cn.betel.softwareinstallhelper.model.Admin;
import cn.betel.softwareinstallhelper.service.AdminLoginService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AdminLoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
public AdminLoginService als= new AdminLoginService();
public String adminname;
public String adminpassword;
public Admin admin;
public String getAdminpassword() {
return adminpassword;
}
public void setAdminpassword(String adminpassword) {
this.adminpassword = adminpassword;
}
public String getAdminname() {
return adminname;
}
public void setAdminname(String adminname) {
this.adminname = adminname;
}
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public String login(){
System.out.println(adminname);
//登录
admin=new Admin();
admin.setAdminname(adminname);
admin.setAdminpassword(adminpassword);
ActionContext ctx = ActionContext.getContext();
ctx.getSession().put("admin", admin);
return als.dologin(adminname, adminpassword);
}
}
service:
public class AdminLoginService {
//登录
public String dologin(String adminname,String adminpassword){
String issuccess="error";
if (adminname==null||adminpassword==null) {
issuccess="error";
}else {
String sql="select * from admin where adminname=?";
System.out.println(sql);
Map<String, String> map=DBControl.queryOne(sql, adminname);
System.out.println(map);
if (map!=null) {
if (map.get("adminpassword").equals(adminpassword)) {
System.out.println(map.get("adminpassword").equals(adminpassword));
//JOptionPane.showMessageDialog(null,"登录成功" );
issuccess="success";
}
}else {
issuccess="error";
}
}
return issuccess;
}
}
配置文件struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="adminlogin"
class="cn.betel.softwareinstallhelper.action.AdminLoginAction" method="login">
<result name="success">/frame_index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>