日期:2014-05-20  浏览次数:20765 次

struts2 类型转换的问题
LoginAction.java
Java code

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;
        }
    }



}



Java code
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;
    }
}

Java code
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;
    }
    
}


LoginAction-conversion.properties

user=com.cz.MyConverter


index.jsp

HTML code
<%@ 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>


struts.xml
XML code
<?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>


报的错是
Java code

严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
    at com.cz.LoginAction.execute(LoginAction.java:30)



不知道自己建的类型转换器为什么没有作用

------解决方案--------------------
30行是哪一行,明显的空指针啊。。。先把30行列出来给看看
------解决方案--------------------
debug看看进到转换器没先,根据情况再做下一步跟踪计划
------解决方案--------------------
LoginAction-conversion.properties 这个文件和对应action在不在一个包下!
------解决方案--------------------
UserBean这个改成User?
------解决方案--------------------
可以一步步排查,比如先不用converter看是否正常
------解决方案--------------------
if(this.getUser().getUsername().equals("cz")&&this.getUser().getPassword().equals("fzqkxeqm")){
你的这种写法很容易出现空指针异常的,你可以考虑使用if("cz".equals(this.getUser().getUsername()) && "fzqkxeqm".equals(this.getUser().getPassword())){
这样可以避免抛出不必要的空指针异常信息,按照你那种写法,用户必须输入数据才能正常提交,否则必然会出空指针异常,而改成我建议的这种写法之后,即使为空也会当成空串处理而不是空指向……反正就那么回事,解释多了反而搞的我自己都解释不清楚了,呵呵
------解决方案--------------------
先判断下是否为null,在进行下面的方法