ssh中struts2的action的编写

来源:互联网 发布:淘宝到了1个钻 编辑:程序博客网 时间:2024/05/19 16:33
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 先配置包结构 --><package name="" extends="struts-default" namespace="/"><action name="" class="" method="{1}"></action></package>    </struts>

package com.itheima.web.action;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.itheima.domain.User;import com.itheima.service.UserService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 用户的控制器 * @author Administrator *///在写action的时候,可以实现Action接口,也可以继承Actionsupport这个类,//Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,//重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,//Actionsupport还实现了很多有用的接口,简化Struts 2的Action开发。//ModelDriven,意思是直接把实体类当成页面数据的收集对象public class UserAction extends ActionSupport implements ModelDriven<User>{private static final long serialVersionUID = -3413092622818913571L;//利用ModelDriven机制,接收页面传过来的user对象属性的值//ModelDriven背后的机制就是ValueStack。界面通过:user_code/user_name/user_password这样的名称,//就能够被直接赋值给user对象,这证明user对象正是ValueStack中的一个root对象!//ModelDrivenInterceptor是缺省的拦截器链的一部分,当一个请求经过ModelDrivenInterceptor的时候,//在这个拦截器中,会判断当前要调用的Action对象是否实现了ModelDriven接口,如果实现了这个接口,//则调用getModel()方法,并把返回值(本例是返回user对象)压入ValueStack。private User user = new User();public User getM.odel() {return user;}//获取userService对象bean
        //引入一个插件的包:        //struts2-spring-plugin-2.3.24.jar        //在这个插件中开启一个Struts2常量        //<constant name="struts.objectFactory" value="spring" />        //默认的情况下struts2将这个常量关闭的,现在引入插件以后,将常量开启了,引发下面的一些常量生效.        //struts.objectFactory.spring.autoWire = name        //那么就可以在Action中提供想注入的属性了        //传统方式的写法        //WebApplicationContext webApplicationContext = WebApplicationContextUtils        //.getWebApplicationContext(ServletActionContext.getServletContext());        //CustomerService customerService = (CustomerService) webApplicationContext.getBean("customerService");        //这种写法很麻烦的,因为需要在每个Action中的每个方法上获取工厂,通过工厂获得类.private UserService userService;public void setUserService(UserService userService) {this.userService = userService;}/** * 注册功能 * @return */public String regist(){// 接收请求参数userService.save(user);return LOGIN;}/** * 通过登录名,判断,登录名是否已经存在 * @return */public String checkCode(){// 调用业务层,查询User u = userService.checkCode(user.getUser_code());// 获取response对象HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=UTF-8");try {// 获取输出流PrintWriter writer = response.getWriter();// 进行判断if(u != null){// 说明:登录名查询到用户了,说明登录已经存在了,不能注册writer.print("no");}else{// 说明,不存在登录名,可以注册writer.print("yes");}} catch (IOException e) {e.printStackTrace();}return NONE;}/** * 登录功能 * @return */public String login(){User existUser = userService.login(user);// 判断,登录名或者密码错误了if(existUser == null){return LOGIN;}else{ServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);// 登录成功return "loginOK";}}/** * 退出功能 * @return */public String exit(){ServletActionContext.getRequest().getSession().removeAttribute("existUser");return LOGIN;}}

 
原创粉丝点击