日期:2014-05-20 浏览次数:20765 次
package com.cz; import com.opensymphony.xwork2.Action; public class LoginAction implements Action{ private UserBean user; private String tip; public void setUser(UserBean user){ this.user=user; } public UserBean getUser(){ return this.user; } public void setTip(String tip){ this.tip=tip; } public String getTip(){ return this.tip; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("hello"); if(this.getUser().getUsername().equals("cz")&&this.getUser().getPassword().equals("fzqkxeqm")){ this.setTip("登录成功"); return SUCCESS; }else{ this.setTip("登录失败"); return ERROR; } } }
package com.cz; import java.util.Map; import ognl.DefaultTypeConverter; public class MyConverter extends DefaultTypeConverter{ //类型转换器必须重写convertValue方法,该方法需要完成双向转换 @Override public Object convertValue(Map context,Object value,Class toType){ if(UserBean.class==toType){ String[] params = (String [])value; UserBean user=new UserBean(); String[] userValues=params[0].split(","); user.setUsername(userValues[0]); user.setPassword(userValues[1]); return user; }else if(String.class==toType){ UserBean user=(UserBean)value; return "<"+user.getUsername()+","+user.getPassword()+">"; } return null; } }
package com.cz; import java.util.Date; public class UserBean { private String username; private String password; public UserBean(){ } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>请输入您的注册信息</h1> <form method="post" action="./hello/login.action"> 用户名:<input type="text" name="username"><p> 密码:<input type="password" name="password"><p> <input type="submit" value="提交"/> </form> </body> </html>
<?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="struts2" namespace="/hello" extends="struts-default"> <!-- Action名字,类以及导航页面定义 --> <!-- 通过Action类处理才导航的Action定义 --> <action name="*" class="com.cz.LoginAction"> <result name="success">/result.jsp</result> <result name="error">/hello/index.jsp</result> </action> </package> </struts>
严重: Servlet.service() for servlet default threw exception java.lang.NullPointerException at com.cz.LoginAction.execute(LoginAction.java:30)