- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 struts2 开发一个示例 Web 应用程序。但我遇到了一些奇怪的行为
CustomerAction.java:
package com.astro.action;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.*;
import javax.servlet.*;
import com.astro.model.*;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
//import org.hibernate.classic.Validatable;
import com.astro.base.framework.AstroSystemException;
import com.astro.bo.CustomerBo;
import com.astro.model.Customer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
public class CustomerAction extends ActionSupport implements Serializable,SessionAware,Preparable, ModelDriven<CustomerPageInfo>{
private static final long serialVersionUID = 2335707118972931221L;
private Map<String,Object> session;
String target = "";
//DI via Spring
private CustomerPageInfo model;
private CustomerBo customerBo;
private Customer customer=new Customer();
public CustomerBo getCustomerBo() {
return customerBo;
}
public void setCustomerBo(CustomerBo customerBo) {
this.customerBo = customerBo;
}
public String errorDisplay()
{
return "input";
}
//Register a Customer
public String register() throws Exception{
System.out.println("I am in register method of CustomerAction.java");
System.out.println("((())))"+model.getCountryList().toString());
System.out.println("((())))"+model.getCustomerInfo());
model.getCustomerInfo().setCreatedDate(new Date());
customerBo.register(customer);
return SUCCESS;
}
public void prepare() throws Exception {
if(model==null)
{
model=new CustomerPageInfo();
System.out.println("In prepare method");
if(model.getCountryList()==null)
{
List<NameValuePair> countryList=customerBo.getAllCountries();
model.setCountryList(countryList);
}
System.out.println("Before initializing"+model.getCustomerInfo());
if(model.getCustomerInfo()==null)
{
model.setCustomerInfo(customer);
}
System.out.println("After initializing"+model.getCustomerInfo());
}
}
@Override
public void setSession(Map<String, Object> map) {
this.session=map;
}
/*public void validate()
{
try {
if(customerBo.doesCustomerExists(customer)==true)
{
addActionError("Customer Alreay Exists");
}
else
{
addActionMessage(INPUT);
}
} catch (AstroSystemException e) {
e.printStackTrace();
}
}
*/
public void setModel(CustomerPageInfo model)
{
this.model=model;
}
public CustomerPageInfo getModel()
{
return model;
}
}
struts.xml:
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<!-- <constant name="struts.freemarker.mru.max.strong.size" value="250" /> -->
<constant name="struts.freemarker.templatesCache.updateDelay"
value="60000" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="astro" extends="struts-default" namespace="/">
<!-- Different types of result types -->
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="registerCustomer" class="customerAction" method="register">
<interceptor-ref name="defaultStack"/>
<result name="success" type="redirectAction">
<param name="actionName">successInformation</param>
</result>
<result name="input" type="tiles">/registration.tiles</result>
</action>
<action name="successInformation" class="com.astro.action.DisplayAction"
method="display">
<interceptor-ref name="defaultStack"/>
<result name="success" type="tiles">/regSuccess.tiles</result>
</action>
<action name="loginCustomer" class="loginAction" method="login">
<result name="success" type="tiles">/loginSuccess.tiles</result>
<result name="input" type="tiles">/loginForm.tiles</result>
</action>
<action name="loginCustomer1" class="loginAction" method="logout">
<result name="success" type="tiles">/loginForm.tiles</result>
<result name="input" type="tiles">/loginForm.tiles</result>
</action>
<action name="login" class="com.astro.action.DisplayAction1"
method="display">
<result name="success" type="tiles">/loginForm.tiles</result>
</action>
<action name="feedback" class="com.astro.action.DisplayAction1"
method="display1">
<result name="success" type="tiles">/contact.tiles</result>
<result name="input" type="tiles">/contact.tiles</result>
</action>
<action name="contactus" class="contactAction" method="contact">
<result name="success" type="tiles">/contactus.tiles</result>
<result name="input" type="tiles">/contactsuccess.tiles
</result>
</action>
</package>
</struts>
customer.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page session="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<style type="text/css">
@import url(css/main.css);
</style>
<style>
.errorMessage {
color: red;
}
.errors {
background-color:#FFCCCC;
border:1px solid #CC0000;
width:400px;
margin-bottom:8px;
font: message-box;
}
.errors li{
list-style: none;
}
</style>
<style type="text/css">
.errorsBg{
background-color:;
color:black;
border: 0px solid;
}
.errorMessage{
padding:4px 8px;
}
table{
border-spacing: 8px;
}
td{
padding:5px;
}
</style>
<script>
/* $(document).ready(function(){
$("input").keydown(function(){
$("input").css("background-color","yellow");
});
$("input").keyup(function(){
$("input").css("background-color","pink");
});
}); */
</script>
</head>
<html>
<head>
</head>
<body bgcolor="green">
<!-- <h1>Welcome to Astro vedic</h1>
<h2>Register a Customer</h2> -->
<div class="errors" style="width: 500px">
<s:token></s:token>
</div>
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:if>
<s:form action="registerCustomer.do" focusElement="focus" method="post" validate="true" namespace="/">
<s:actionerror />
<s:textfield id="focus" name="name" key="name" size="20" />
<s:password name="password" label="Password "/>
<s:password name="confirmPassword" label="Re-enter Password "/>
<s:textfield name="email" key="email" size="20" required="true" />
<s:textfield name="telephone" key="telephone" size="20"></s:textfield>
<s:select required="true" name="countryCode" label="CountryName" listValue="name" headerKey="" headerValue="Choose One"
list="countryList" listKey="value"/>
<s:submit align="center" key="addCustomer"/>
</s:form>
</div>
</body>
</html>
当我运行代码时,它将准备方法,但不会注册方法。不仅如此,如果我删除 customerAction-validation.xml 那么它会注册方法,但数据库中不会保存任何内容。
Prepare 方法被调用两次。我不明白如何调用注册方法和保存客户详细信息。
最佳答案
在prepare()
中检查model
;
model
将由 ParamsInterceptor 设置(ModelDrivenInterceptor 也应该参与其中,不知道如何,因为我从未使用过ModelDriven),它的运行晚于 Prepare Interceptor,如您在 defaultStack
定义中所见;
您需要使用一个名为paramsPrepareParamsStack的堆栈,其中包含两次Params Interceptor,一个在Prepare Interceptor之前,一个在Prepare Interceptor之后,让 prepare()
方法使用来自页面的参数(模型或不)。
http://struts.apache.org/release/2.3.x/docs/interceptors.html
An example of the paramsPrepareParams trick. This stack is exactly the same as the defaultStack, except that it includes one extra interceptor before the prepare interceptor: the params interceptor.
This is useful for when you wish to apply parameters directly to an object that you wish to load externally (such as a DAO or database or service layer), but can't load that object until at least the ID parameter has been loaded. By loading the parameters twice, you can retrieve the object in the prepare() method, allowing the second params interceptor to apply the values on the object.
关于java - 无法理解为什么在准备后没有调用注册方法。总是调用 customeraction-validation.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489288/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!