关于struts2 的action到底怎么写
关于struts2的action到底怎么写。。。。请对照1的说一下,然后给个例子。。。。还有我们老师写的是这样的。。。跟网上的好多写的都不一样。麻烦大哥们给解释一下pac...
关于struts2的action到底怎么写。。。。请对照1的说一下,然后给个例子。。。。还有我们老师写的是这样的。。。跟网上的好多写的都不一样。麻烦大哥们给解释一下
package action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import biz.LoginBiz;
import dao.UserDao;
import entity.DepartmentInfo;
import entity.UserInfo;
public class LoginAction implements ServletRequestAware,
ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private LoginBiz loginbiz;
private UserInfo userinfo;
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public void setLoginbiz(LoginBiz loginbiz) {
this.loginbiz = loginbiz;
}
public UserInfo getUserinfo() {
return userinfo;
}
public void setUserinfo(UserInfo userinfo) {
this.userinfo = userinfo;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
//方法
//登录
public String selByName(){
HttpSession session=request.getSession();
UserInfo user=this.loginbiz.selByName(userinfo.getUserName(), userinfo.getUserPassword());
if(user==null){
request.setAttribute("msg", "<script>alert('用户名或密码错误');</script>");
return "login";
}
else{
session.setAttribute("user", user);
return "index";
}
}
} 展开
package action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import biz.LoginBiz;
import dao.UserDao;
import entity.DepartmentInfo;
import entity.UserInfo;
public class LoginAction implements ServletRequestAware,
ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private LoginBiz loginbiz;
private UserInfo userinfo;
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public void setLoginbiz(LoginBiz loginbiz) {
this.loginbiz = loginbiz;
}
public UserInfo getUserinfo() {
return userinfo;
}
public void setUserinfo(UserInfo userinfo) {
this.userinfo = userinfo;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
//方法
//登录
public String selByName(){
HttpSession session=request.getSession();
UserInfo user=this.loginbiz.selByName(userinfo.getUserName(), userinfo.getUserPassword());
if(user==null){
request.setAttribute("msg", "<script>alert('用户名或密码错误');</script>");
return "login";
}
else{
session.setAttribute("user", user);
return "index";
}
}
} 展开
2个回答
展开全部
这个还是用的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>
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>
展开全部
呵呵,这个要看你的Struts-config.xml里面是怎么配置的了,你可以在配置文件里面配置Action的参数,name="这里是你要访问的Action的名字,也就是你在from表单里面的" class="这里是你的Action的路径" method="这个是你在Action里面的你要访问的方法名字" 。比如:<action name="upload" class="com.hp.action.UploadAction" method="upload"> 那么from里面就是:<s:form action="upload/upload" method="post"/> 呵呵,希望你能够成功哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询