使用IDE:eclipse;
struts2的版本:2.3.4.1
登录界面代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>login</title> </head> <body> <center> <h1>login platform</h1> <s:form action="/login" method="post" > <table> <tr><td>username:</td> <td><s:textfield name="user.username" ></s:textfield> </td></tr> <tr><td>password:</td> <td><s:textfield name="user.password" ></s:textfield> </td></tr> <tr><td>age:</td> <td><s:textfield name="user.age" ></s:textfield> </td></tr> <tr><td>isBoy:</td> <td><s:textfield name="user.isBoy" ></s:textfield> </td></tr> <tr> <td colspan="2"><s:submit value="login" ></s:submit> </td></tr> </table> </s:form> </center> </body> </html>
对应的bean中有一个成员变量是:private boolean isBoy;(是boolean 类型的)
package com.jn.bean; public class LoginUser { private int age; private String username; private String password; /*** * 是否是男性 */ private boolean isBoy; public LoginUser() { super(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; } public boolean isBoy() { return isBoy; } public void setBoy(boolean isBoy) { this.isBoy = isBoy; } }
其中的getter,setter方法都是使用eclipse自动生成的。
对应的action为:
package com.jn.action; import com.jn.bean.LoginUser; import com.opensymphony.xwork2.ActionSupport; public class LoginVerifyAction extends ActionSupport { private static final long serialVersionUID = -5532496750392156543L; private LoginUser user; @Override public String execute() throws Exception { System.out.println("user information:"); System.out.println("username:"+user.getUsername()); System.out.println("password:"+user.getPassword()); System.out.println("age:"+user.getAge()); System.out.println("isBoy:"+user.isBoy()); return super.execute(); } public LoginUser getUser() { return user; } public void setUser(LoginUser user) { this.user = user; } }
但是登录时报错:
警告: Error setting expression 'user.isBoy' with value '[Ljava.lang.String;@863941' ognl.NoSuchPropertyException: com.jn.bean.LoginUser.isBoy [java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class com.jn.bean.LoginUser with modifiers "private"] at ognl.OgnlRuntime.setFieldValue(OgnlRuntime.java:1682) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:87) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162) at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315) at ognl.ASTProperty.setValueBody(ASTProperty.java:127) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.ASTChain.setValueBody(ASTChain.java:227) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.Ognl.setValue(Ognl.java:737) at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:217) at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:186) at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:173) at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:151) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:317) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:228) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class com.jn.bean.LoginUser with modifiers "private" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) at java.lang.reflect.Field.doSecurityCheck(Field.java:960) at java.lang.reflect.Field.getFieldAccessor(Field.java:896) at java.lang.reflect.Field.set(Field.java:657) at ognl.OgnlRuntime.setFieldValue(OgnlRuntime.java:1674) ... 67 more /-- Encapsulated exception ------------\ java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class com.jn.bean.LoginUser with modifiers "private" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) at java.lang.reflect.Field.doSecurityCheck(Field.java:960) at java.lang.reflect.Field.getFieldAccessor(Field.java:896) at java.lang.reflect.Field.set(Field.java:657) at ognl.OgnlRuntime.setFieldValue(OgnlRuntime.java:1674) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:87) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162) at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315) at ognl.ASTProperty.setValueBody(ASTProperty.java:127) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.ASTChain.setValueBody(ASTChain.java:227) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.Ognl.setValue(Ognl.java:737) at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:217) at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:186) at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:173) at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:151) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:317) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:228) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) \--------------------------------------/
错误信息说明,struts2没有找到isBoy 的setter方法,因为struts2认为isBoy 的setter方法名应该是setIsBoy
为什么会设置isBoy时就有问题呢?因为按照struts2 的依赖注入规则,setter方法应该是set+成员变量(首字母变为大写)。
我把isBoy的setter方法由setBoy 修改为setIsBoy,就好了。
public void setIsBoy(boolean isBoy) { this.isBoy = isBoy; }
总结:如果要依赖注入的属性是boolean类型并且属性名以is开头,就会出现上述问题。
解决方法:
如果使用struts2的依赖注入,那么boolean 类型的属性名不要以is开头。我修改方法是把属性名由isBoy改为boy:
package com.jn.bean; public class LoginUser { private int age; private String username; private String password; /*** * 是否是男性 */ private boolean boy; public LoginUser() { super(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; } public boolean isBoy() { return boy; } public void setBoy(boolean boy) { this.boy = boy; } }
登录页面中把user.isBoy 改为user.boy 就好了。
相关推荐
Struts2的内建类型转换涵盖了大部分常见类型,包括但不限于String、boolean/Boolean、char/Character、整数类型(int/Integer、float/Float、long/Long、double/Double)、日期(使用当前时区的SHORT格式)以及数组...
Struts2提供了一种机制,能够自动将这些字符串转换为相应的数据类型,如Integer、Date、Boolean等。 2. **Struts2类型转换器** Struts2的核心组件`ValueStack`负责管理Action对象,并使用`ConversionService`接口...
Struts2提供了一系列内置的Converter,可以处理基本类型和常用的Java对象,如Date、Boolean等。同时,Struts2也允许开发者自定义Converter来处理特定的业务需求。 在Struts2中,类型转换的过程分为两个步骤: 1. ...
此外,Struts2还支持如`@Param`注解来处理HTTP请求参数,`@Inject`或`@Autowired`注解来进行依赖注入,提高代码复用和解耦。 在实际项目中,使用Struts2注解进行登录处理可以大大简化开发流程,使代码更易于理解和...
在这个例子中,Struts2会自动尝试将`uname`、`upass`、`age`和`birthday`字段的字符串值转换为对应的用户类属性类型,如`String`、`String`、`int`和`Date`。对于`hobby`字段,Struts2会将选中的复选框值转换为一个...
基本类型的属性注入是Spring中最常见的用法之一,它支持所有的Java基本数据类型如`int`、`long`、`double`、`boolean`等,也包括它们对应的包装类如`Integer`、`Long`、`Double`、`Boolean`等。例如: ```xml ...
Struts2是一个基于MVC(Model-View-Controller)设计模式的Java web应用程序框架,它在Java社区中广泛应用于构建动态、数据驱动的Web应用。本实例将详细讲解如何使用Struts2实现一个简单的登录功能。 一、Struts2...
首先,我们需要在开发环境中创建一个新的Java Web项目,并引入Struts2的依赖库,通常包括struts2-core、struts2-convention-plugin等相关jar包。 三、配置Struts2 1. `struts.xml`:这是Struts2的核心配置文件,...
Struts2提供了一系列内置的类型转换器,可以处理基本类型如`int`、`double`、`boolean`以及Java集合类型如`List`、`Map`等。此外,还支持日期和时间的转换,如`java.util.Date`。 4. **类型转换的优先级** 类型...
Struts2框架通过拦截器处理请求,拦截器链可以实现诸如参数绑定、类型转换、异常处理等功能。在登录案例中,Struts2会自动将表单数据绑定到Action类的属性上,无需手动获取请求参数。 总结一下,Struts2登录案例...
Struts2内置了一系列常见的类型转换器,可以处理基本类型如String、boolean、char、int、long、float、double以及Date,还包括数组和集合(假定元素是String类型)。当表单提交的数据与Action类中的字段类型匹配时,...
首先,你需要在项目中引入Struts2的依赖库。接着,配置struts2的核心过滤器`<filter>`和`<filter-mapping>`。然后,定义一个Action类,通常继承自`ActionSupport`,并实现业务逻辑方法。在struts.xml配置文件中,...
Struts2内建了一套类型转换机制,可以自动将字符串转换为以下基本类型:boolean、char、int、long、float、double、Date以及它们对应的包装类。此外,它还可以处理数组和集合,如ArrayList、LinkedList等。例如,...
Struts2是一个强大的Java web应用程序框架,用于构建和部署企业级的、基于MVC(Model-View-Controller)模式的应用程序。在这个“Struts2小例子”中,我们将深入理解Struts2的核心概念以及如何通过它来实现一个简单...
对于8个基本数据类型(byte, short, int, long, float, double, char, boolean)及其对应的包装类,以及Date类型,Struts2都会尝试进行自动类型转换。 例如,如果表单提交了一个日期格式的字符串"2022-01-01",...
这里,`LoginAction` Bean注入了`loginService`,实现了依赖注入。 7. **编写Java类** 创建相应的Java类,包括`LoginAction`、`Check`接口和`LoginCheck`实现类。`LoginAction`负责处理用户请求,调用`...
此外,Struts2提供了丰富的结果类型和插件支持,提高了开发效率。 2. **创建Action类**: 首先,我们需要创建一个Action类,通常继承自`ActionSupport`类或自定义的基类。Action类是业务逻辑的载体,它包含了处理...