前导博文
JavaWeb_(SSH)使用Struts框架实现用户的登陆
第一种DefaultAction什么都没有干
第二种实现了Action接口
第三种继承了ActionSupport类【常用】
目录结构
package com.Gary.web;public class DefaultAction { public String execute() { System.err.println("这是一个普通的action"); return null; } }
package com.Gary.web;import com.opensymphony.xwork2.Action;class ImplAction implements Action{ @Override public String execute() throws Exception { System.out.println("这是实现了Action接口的action"); return null; }}
package com.Gary.web;import org.apache.struts2.ServletActionContext;import com.Gary.domain.User;import com.Gary.service.UserService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class UserAction extends ActionSupport implements ModelDriven{ public User user = new User(); public String execute() throws Exception { System.err.println("我已经运行了"); UserService userService = new UserService(); boolean success = userService.findUser(user); if(success) { return "success"; }else{ ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!"); return "error"; } } @Override public User getModel() { // TODO Auto-generated method stub return user; }}
配置struts.xml
/index.html /login.jsp
/index.html /login.jsp