我正在使用spring Mvc Framework。我的控制器类看起来像这样。
public class UpdateStockController extends AbstractWizardFormController {
StockRegisterService stockRegisterService;
DimensionStoneService dimensionStoneService;
MineralService mineralService;
AutoPopulatingList openDimensionStoneBeans=new AutoPopulatingList(DimensionStoneBean.class);
AutoPopulatingList todayDimensionStoneBeans=new AutoPopulatingList(DimensionStoneBean.class);
StockRegister stockRegister;
List<DimensionStone> openDimensionStones=new ArrayList<DimensionStone>();
List<DimensionStone> todayDimensionStones=new ArrayList<DimensionStone>();
float cumulativeStockOrg;
boolean flag1;
boolean flag2;
public StockRegisterService getStockRegisterService() {
return stockRegisterService;
}
public void setStockRegisterService(StockRegisterService stockRegisterService) {
this.stockRegisterService = stockRegisterService;
}
public DimensionStoneService getDimensionStoneService() {
return dimensionStoneService;
}
public void setDimensionStoneService(DimensionStoneService dimensionStoneService) {
this.dimensionStoneService = dimensionStoneService;
}
public MineralService getMineralService() {
return mineralService;
}
public void setMineralService(MineralService mineralService) {
this.mineralService = mineralService;
}
public UpdateStockController(){
setSessionForm(true);
}
@Override
protected Object formBackingObject(HttpServletRequest request)throws Exception {
String applicationId=null;
String serviceId=null;
try{
applicationId=(String)request.getParameter("appId").trim();
serviceId=(String)request.getParameter("serviceId").trim();
}catch (NullPointerException e) {
HttpSession session=request.getSession();
applicationId=(String) session.getAttribute("applicationId");
serviceId=(String) session.getAttribute("serviceId");
}
String mineralId=(String)request.getParameter("mineralIdAndName").trim().split(":")[0];
String mineralName=(String)request.getParameter("mineralIdAndName").trim().split(":")[1];
if(mineralId.equals("10")){
flag2=true;
}
System.out.println("..............................flag2 : "+flag2+".......................");
System.out.println("..............................flag1 : "+flag1+".......................");
HttpSession session=request.getSession();
session.setAttribute("applicationId", applicationId);
session.setAttribute("serviceId",serviceId);
Date productionDate=new Date();
UpdateStockBean updateStockBean=new UpdateStockBean();
updateStockBean.setMineralId(mineralId);
updateStockBean.setMineralName(mineralName);
updateStockBean.setAccidentOccured("NO");
updateStockBean.setCompPaid("NO");
stockRegister=new StockRegister();
@SuppressWarnings("rawtypes")
List ckeckTodayStockRegisterList=stockRegisterService.retrieveDataFromStockRegister(applicationId,productionDate,mineralId);
if(ckeckTodayStockRegisterList.isEmpty()){
System.out.println("..............................If1.......................");
@SuppressWarnings("rawtypes")
List openingBalFromPreviousStockRegisterList=stockRegisterService.retrieveOpeningBalance(applicationId,mineralId);
flag1=true;
if (!openingBalFromPreviousStockRegisterList.isEmpty()) {
System.out.println("..............................If2.......................");
flag1=false;
stockRegister=(StockRegister)openingBalFromPreviousStockRegisterList.get(0);
stockRegister.setStockRegisterId(0);
if(stockRegister.getBalanceQuantity()!=null){
updateStockBean.setOpenbalance(stockRegister.getBalanceQuantity());
}
cumulativeStockOrg=stockRegister.getCumulativeStock();
System.out.println(".............................."+cumulativeStockOrg+".......................");
}
}else{
System.out.println("..............................else.......................");
stockRegister=(StockRegister) ckeckTodayStockRegisterList.get(0);
long stockRegId=stockRegister.getStockRegisterId();
openDimensionStones=dimensionStoneService.getOpenDimensionStones(stockRegId);
todayDimensionStones=dimensionStoneService.getTodayDimensionStones(stockRegId);
updateStockBean.setOpenbalance(stockRegister.getOpeningBal());
updateStockBean.setTodayproduction(stockRegister.getQuantityProduced());
updateStockBean.setNoOfMale(stockRegister.getNoOfMaleEmp());
updateStockBean.setNoOfFemale(stockRegister.getNoOfFemaleEmp());
updateStockBean.setAccidentOccured(stockRegister.getAccidentOccured());
updateStockBean.setDescriptionAccident(stockRegister.getAccidentDescription());
updateStockBean.setCompPaid(stockRegister.getCompensationPaid());
updateStockBean.setCompDetails(stockRegister.getCompensationDet());
updateStockBean.setCompAmount(stockRegister.getCompensationAmt());
cumulativeStockOrg=stockRegister.getCumulativeStock()-stockRegister.getQuantityProduced();
for (Iterator<DimensionStone> iterator = openDimensionStones.iterator(); iterator.hasNext();) {
System.out.println("..............................Haii77777777777.......................");
DimensionStone dimensionStone=iterator.next();
DimensionStoneBean dimensionStoneBean=new DimensionStoneBean();
dimensionStoneBean.setStoneNo(Integer.parseInt(dimensionStone.getStoneNo().toString()));
dimensionStoneBean.setLength(dimensionStone.getLength());
dimensionStoneBean.setBreadth(dimensionStone.getBreadth());
dimensionStoneBean.setHeight(dimensionStone.getHeight());
dimensionStoneBean.setDimension(dimensionStone.getDimension());
dimensionStoneBean.setIsIssued(dimensionStone.getIsIssued());
openDimensionStoneBeans.add(dimensionStoneBean);
}
updateStockBean.setOpenDimensionStones(openDimensionStoneBeans);
for (Iterator<DimensionStone> iterator = todayDimensionStones.iterator(); iterator.hasNext();) {
System.out.println("..............................Haiiii8888888888888.......................");
DimensionStone dimensionStone=iterator.next();
DimensionStoneBean dimensionStoneBean=new DimensionStoneBean();
dimensionStoneBean.setStoneNo(Integer.parseInt(dimensionStone.getStoneNo().toString()));
dimensionStoneBean.setLength(dimensionStone.getLength());
dimensionStoneBean.setBreadth(dimensionStone.getBreadth());
dimensionStoneBean.setHeight(dimensionStone.getHeight());
dimensionStoneBean.setDimension(dimensionStone.getDimension());
dimensionStoneBean.setIsIssued(dimensionStone.getIsIssued());
todayDimensionStoneBeans.add(dimensionStoneBean);
}
updateStockBean.setTodayDimensionStones(todayDimensionStoneBeans);
}
return updateStockBean;
}
@SuppressWarnings("unchecked")
@Override
protected ModelAndView processFinish(HttpServletRequest req,HttpServletResponse res, Object cmd, BindException be)
throws Exception {
float cumulativeStock;
float dimensionCumulativeVolume=cumulativeStockOrg;
UpdateStockBean bean = (UpdateStockBean)cmd;
Long openingBalance=bean.getOpenbalance();
Long quantityProduced=(Long)bean.getTodayproduction();
Long totalQuantity=openingBalance+quantityProduced;
Long balanceQuantity=totalQuantity;
System.out.println("..............................flag2 : "+flag2+".......................finish");
System.out.println("..............................flag1 : "+flag1+".......................finish");
if(flag1){
System.out.println("..............................flag true.......................1");
cumulativeStock=(Float.parseFloat(openingBalance.toString())+Float.parseFloat(quantityProduced.toString()));
}else{
System.out.println("..............................flag false.......................1");
cumulativeStock=cumulativeStockOrg+quantityProduced;
}
System.out.println(".............................."+cumulativeStock+".......................1");
if(stockRegister.getStockRegisterId()==0){
stockRegister.setStockRegisterId(stockRegisterService.getMaximumValueStockRegisterId()+1);
}
Application application=new Application();
application.setApplicationId(Long.parseLong(req.getSession().getAttribute("applicationId").toString().trim()));
stockRegister.setApplication(application);
stockRegister.setProductionDate(new SimpleDateFormat("dd-MMM-yyyy").parse(bean.getProductiondate()));
stockRegister.setOpeningBal(openingBalance);
stockRegister.setMineralId(Long.parseLong(bean.getMineralId()));
stockRegister.setQuantityProduced(quantityProduced);
stockRegister.setTotalQuantity(totalQuantity);
stockRegister.setBalanceQuantity(balanceQuantity);
stockRegister.setNoOfFemaleEmp((Integer)bean.getNoOfFemale());
stockRegister.setNoOfMaleEmp(bean.getNoOfMale());
stockRegister.setAccidentOccured(bean.getAccidentOccured());
stockRegister.setAccidentDescription(bean.getDescriptionAccident());
stockRegister.setCompensationPaid(bean.getCompPaid());
stockRegister.setCompensationAmt(bean.getCompAmount());
stockRegister.setCompensationDet(bean.getCompDetails());
stockRegister.setCumulativeStock(cumulativeStock);
System.out.println(".............................."+cumulativeStock+".......................2");
dimensionStoneService.deleteDimensionStonesOnToday(openDimensionStones);
dimensionStoneService.deleteDimensionStonesOnToday(todayDimensionStones);
openDimensionStoneBeans=bean.getOpenDimensionStones();
if(openDimensionStoneBeans.size()>bean.getOpenbalance()){
int size=openDimensionStoneBeans.size();
int j=(int) bean.getOpenbalance();
for(int i=(int) bean.getOpenbalance();i<size;i++){
openDimensionStoneBeans.remove(j);
}
bean.setOpenDimensionStones(openDimensionStoneBeans);
cmd=(Object)bean;
}
openDimensionStones=new ArrayList<DimensionStone>();
for (Iterator<DimensionStoneBean> iterator = openDimensionStoneBeans.iterator(); iterator.hasNext();) {
System.out.println("..............................Haiiiii11111.......................");
DimensionStoneBean dimensionStoneBean = iterator.next();
DimensionStone dimensionStone=new DimensionStone();
dimensionStone.setStoneNo(Long.parseLong(((Integer)dimensionStoneBean.getStoneNo()).toString()));
dimensionStone.setLength(dimensionStoneBean.getLength());
dimensionStone.setBreadth(dimensionStoneBean.getBreadth());
dimensionStone.setHeight(dimensionStoneBean.getHeight());
dimensionStone.setDimension(dimensionStoneBean.getDimension());
dimensionStone.setIsIssued(dimensionStoneBean.getIsIssued());
dimensionStone.setStockType("opening");
dimensionStone.setStockRegisterId(stockRegister.getStockRegisterId());
dimensionCumulativeVolume=dimensionCumulativeVolume+dimensionStoneBean.getDimension();
openDimensionStones.add(dimensionStone);
}
todayDimensionStoneBeans=bean.getTodayDimensionStones();
if(todayDimensionStoneBeans.size()>bean.getTodayproduction()){
int size=todayDimensionStoneBeans.size();
int j=(int) bean.getTodayproduction();
for(int i=(int) bean.getTodayproduction();i<size;i++){
todayDimensionStoneBeans.remove(j);
}
bean.setTodayDimensionStones(todayDimensionStoneBeans);
cmd=(Object)bean;
}
todayDimensionStones=new ArrayList<DimensionStone>();
for (Iterator<DimensionStoneBean> iterator = todayDimensionStoneBeans.iterator(); iterator.hasNext();) {
System.out.println("..............................Haiiiii22222.......................");
DimensionStoneBean dimensionStoneBean = iterator.next();
DimensionStone dimensionStone=new DimensionStone();
dimensionStone.setStoneNo(Long.parseLong(((Integer)dimensionStoneBean.getStoneNo()).toString()));
dimensionStone.setLength(dimensionStoneBean.getLength());
dimensionStone.setBreadth(dimensionStoneBean.getBreadth());
dimensionStone.setHeight(dimensionStoneBean.getHeight());
dimensionStone.setDimension(dimensionStoneBean.getDimension());
dimensionStone.setIsIssued(dimensionStoneBean.getIsIssued());
dimensionStone.setStockType("current");
dimensionStone.setStockRegisterId(stockRegister.getStockRegisterId());
dimensionCumulativeVolume=dimensionCumulativeVolume+dimensionStoneBean.getDimension();
todayDimensionStones.add(dimensionStone);
}
if(flag2){
System.out.println(".............................."+dimensionCumulativeVolume+".......................3");
stockRegister.setCumulativeStock(dimensionCumulativeVolume);
}
stockRegisterService.saveOrupdateStockRegister(stockRegister);
dimensionStoneService.saveOrupdateDimensionStones(openDimensionStones);
openDimensionStoneBeans.clear();
dimensionStoneService.saveOrupdateDimensionStones(todayDimensionStones);
todayDimensionStoneBeans.clear();
HttpSession session=req.getSession();
String appId=(String) session.getAttribute("applicationId");
String serviceId=(String) session.getAttribute("serviceId");
String mineralIdAndName=bean.getMineralId()+":"+bean.getMineralName();
return new ModelAndView(new InternalResourceView("updateStock.c?appId="+appId+"&serviceId="+serviceId+"&mineralIdAndName="+mineralIdAndName));
}
@Override
protected ModelAndView processCancel(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors)
throws Exception {
UpdateStockBean bean=(UpdateStockBean)command;
String cancelCheck=request.getParameter("cancelCheck");
HttpSession session=request.getSession();
String appId=(String) session.getAttribute("applicationId");
String serviceId=(String) session.getAttribute("serviceId");
String mineralIdAndName=bean.getMineralId()+":"+bean.getMineralName();
if(cancelCheck.equals("1")){
response.sendRedirect("tabs.c?action=home_tab_emp");
return new ModelAndView(new InternalResourceView("tabs.nic?action=home_tab_emp"));
}else if(cancelCheck.equals("2")){
return new ModelAndView(new InternalResourceView("updateStock.c?appId="+appId+"&serviceId="+serviceId+"&mineralIdAndName="+mineralIdAndName));
}else{
response.sendRedirect("tabs.nic?action=mineralSelect&appId="+appId+"&serviceId="+serviceId);
return new ModelAndView(new InternalResourceView("tabs.c?action=mineralSelect&appId="+appId+"&serviceId="+serviceId));
}
}
@Override
protected void validatePage(Object command, Errors errors, int page) {
UpdateStockValidator updateStockValidator=new UpdateStockValidator();
switch(page){
case 0:
updateStockValidator.validateFormStart(command, errors);
break;
case 1:
break;
case 2:
updateStockValidator.validateForm2(command, errors);
break;
case 3:
updateStockValidator.validateForm3(command, errors);
break;
case 4:
updateStockValidator.validateFormStart(command, errors);
break;
}
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked"})
protected Map referenceData(HttpServletRequest request, Object command,Errors errors, int page) throws Exception {
Map map = new HashMap();
UpdateStockBean bean = (UpdateStockBean)command;
String applicationId=(String) request.getSession().getAttribute("applicationId");
String mineralId=bean.getMineralId();
Date productionDate=new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(productionDate);
cal.add(Calendar.DAY_OF_MONTH, -30);
Date today30 = cal.getTime();
map.put("stockRegisters", stockRegisterService.stockRegisterBetweenTwoDaysWithApplicationId(applicationId,mineralId,today30,productionDate));
map.put("ckeckOpeningBalance",stockRegisterService.retrieveDataBeforeProductionDate(applicationId,mineralId,productionDate));
map.put("ckeckDimensionStone",mineralService.getMineralByApplicationId(applicationId));
map.put("mineralList",mineralService.getMineralObjectsByApplicationId(applicationId));
openDimensionStoneBeans=bean.getOpenDimensionStones();
if(openDimensionStoneBeans.size()>bean.getOpenbalance()){
int size=openDimensionStoneBeans.size();
int j=(int) bean.getOpenbalance();
for(int i=(int) bean.getOpenbalance();i<size;i++){
openDimensionStoneBeans.remove(j);
}
bean.setOpenDimensionStones(openDimensionStoneBeans);
command=(Object)bean;
}
todayDimensionStoneBeans=bean.getTodayDimensionStones();
if(todayDimensionStoneBeans.size()>bean.getTodayproduction()){
int size=todayDimensionStoneBeans.size();
int j=(int) bean.getTodayproduction();
for(int i=(int) bean.getTodayproduction();i<size;i++){
todayDimensionStoneBeans.remove(j);
}
bean.setTodayDimensionStones(todayDimensionStoneBeans);
command=(Object)bean;
}
return map;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected ModelAndView showForm(HttpServletRequest req,HttpServletResponse res, BindException be) throws Exception {
HttpSession ses = req.getSession();
Map pageMap = new HashMap();
pageMap.put("erType", "");
pageMap.put("pageType", "");
if(ses.getAttribute("connection") == null){
pageMap.put("erType", "sessionExpired");
//pageMap.put("pageType", "inner");
return new ModelAndView("page1",pageMap);
}
try{
if(ses.getAttribute("loginType").toString().trim().equals("Employee")){
}else if(ses.getAttribute("loginType").toString().trim().equals("Applicant")){
}else if(ses.getAttribute("loginType").toString().trim().equals("Agent")){
}
}catch (Exception e){e.printStackTrace();return new ModelAndView(new InternalResourceView("login.nic"));}
return super.showForm(req, res, be);
}
}
这里是变量
List<DimensionStone> openDimensionStones=new ArrayList<DimensionStone>();
List<DimensionStone> todayDimensionStones=new ArrayList<DimensionStone>();
float cumulativeStockOrg;
boolean flag1;
boolean flag2;
是初始化的,它们在执行后没有被清除。那个变量的值也存在于另一个执行中。结果程序逻辑无法正常工作
配置文件是
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="updateStock.c">UpdateStockController</prop>
</props>
</property>
</bean>
<bean id="UpdateStockController" class="mams.web.UpdateStockController">
<property name="commandName"><value>updateStock</value></property>
<property name="commandClass">
<value>mams.web.beans.UpdateStockBean</value>
</property>
<property name="stockRegisterService" ref="StockRegisterService"></property>
<property name="dimensionStoneService" ref="DimensionStoneService"></property>
<property name="mineralService" ref="MineralService"></property>
<property name="validator" ref="UpdateStockValidator"></property>
<property name="pages">
<list>
<!-- <value>showMineralsForStock</value>-->
<value>updateStock</value>
<value>addOrEditOpenBalane</value>
<value>addOrEditOpenDimensionStone</value>
<value>addOrEditTodayDimensionStone</value>
<value>updateStock</value>
</list>
</property>
</bean>
答案 0 :(得分:3)
Spring的控制器是单身(就像servlet一样)。所有用户共享的每种控制器类型只有一个实例。它们必须stateless才能保证线程安全。
所以只需将您的类字段移动到相应的方法中,问题就会消失。
正如Bhushan Bhangale所提到的,如果你想在HTTP请求之间保留同一用户的某个变量的状态,那么你可以将这个变量存储在HTTP会话中。
答案 1 :(得分:3)
Spring控制器是单例,所以如果你在没有线程安全实现的情况下依赖于方法执行中的全局变量那么你就错了。
您会遇到意外行为。
将这些变量设置为您需要的方法的本地变量,您应该没问题。
答案 2 :(得分:1)
正如其他人所说,你的控制器应该是无状态的(即不依赖类变量来存储状态)。
我注意到你在不同的方法中使用变量 - 如果你希望在请求之间存储状态,通常的方法是使用会话对象来存储你的数据。