struts2局部类型转换器

    局部类型转换器,弄了挺长时间,主要的错误是报空指针的错误。在网上百度了不少的文章,出现错误的原因自己归纳为以下的几条:1.局部类型转换器的配置文件...properties文件放置位置不正确。它应该和相应的action放置在同一个包下。2. action的命名和properties文件的命名action不一致,或在网页中提交的action的命名和上述两文件命名不一致。3. 类型转换器的properties文件的内容配置错误。如果避免上述的错误,程序应该差不多可以正常运行。自己的代码贴一下,主要内容是在一个文本框中输入一个字符串,通过action和局部类型转换器后将用户名和密码分开显示,具体代码如下(环境MyEclipse):
    index.jsp的网页代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>
  <s:form action="UserAction.action" method="post">
   <s:textfield name="user" label="username and password"></s:textfield>
   <s:submit value="提交" ></s:submit>
   </s:form>
  </body>
</html>
    提交的UserAction代码:
package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction  extends ActionSupport{

private User user;
private String tip;
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public User getUser() {
return user;
}
public void setUser(User user) {
  this.user = user;
}
public  String execute() throws Exception{

if(user.getUsername().equals("hh")&& user.getPassword().equals("1")){
setTip("转向成功");
return SUCCESS;
}else {
setTip("转向失败");
return "input";
}
}}
    User的代码:
package com.bean;

public class User {
private String username;
private String password;
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;
}
}
  转换器UserActionConverter的代码:
  package com.action;

import java.util.Map;
import com.bean.User;
import ognl.DefaultTypeConverter;
public class UserActionConverter extends DefaultTypeConverter {
public Object convertValue(Map context,Object value,Class toType){
if(toType==User.class){
String[] params=(String[])value;
User user=new User();
String[] userValues=params[0].split(",");
user.setUsername(userValues[0]);
user.setPassword(userValues[1]);
return user;
}else if(toType==String.class){
User user=(User) value;
return "<"+user.getUsername()+","+user.getPassword()+">";
}
return null;
}
}
    struts.xml文件代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="com.action" extends="struts-default">
<action name="UserAction" class="com.action.UserAction">
<result name="success">/success.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>   
web的配置文件代码(自动生成的):
  <?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>
   跳转成功success.jsp代码:
<%@ 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>
    OK<br>
    ${user.username}<br/>
    ${user }<br/>
    ${user.password }<br>
    ${tip }
  </body>
</html>
error.jsp自己配置下。
    重要的.properties文件UserAction-conversion.properties的代码如下:
    user=com.action.UserActionConverter
    全部代码完毕。
   

你可能感兴趣的:(jsp,bean,struts,MyEclipse,百度)