读书人

初学struts的小疑点JSP中的代码怎么

发布时间: 2012-01-28 22:06:14 作者: rapoo

初学struts的小问题,JSP中的代码如何用struts 分离开。。。。 解决马上给分。
以下是我的JSP页面:
<body>
<%
if(application.getAttribute( "count ")==null){
application.setAttribute( "count ", "1 ");
out.println( "first user ");
out.print(application.getAttribute( "count "));
}
else{
int a=Integer.valueOf((String)application.getAttribute( "count "));
application.setAttribute( "count ",Integer.toString(a+1));
out.print(application.getAttribute( "count "));
}
%>
</body>
我想把这些代码用struts分离开,可是不知道这些代码往哪里写了,麻烦大哥帮我一下,最好有详细的代吗,有的人告诉我说写一个bean,可是这个bean怎么写呀?


[解决办法]
<%@ page language= "java " pageEncoding= "ISO-8859-1 "%>
<%@ taglib uri= "/WEB-INF/struts-logic.tld " prefix= "logic " %>
<%@ taglib uri= "/WEB-INF/struts-template.tld " prefix= "template " %>
<%@ taglib uri= "/WEB-INF/struts-bean.tld " prefix= "bean " %>
<%@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %>
<html>
<head>
<title> JSP for UserLoginForm form </title>
</head>
<body>
<html:form action= "/userLogin ">
password : <html:password property= "password "/> <html:errors property= "password "/> <br/>
userName : <html:text property= "userName "/> <html:errors property= "userName "/> <br/>
<html:submit/> <html:cancel/>

</html:form>
</body>
</html>


-------------------------

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.UserLoginForm;

/**
* MyEclipse Struts
* Creation date: 02-11-2007
*
* XDoclet definition:
* @struts.action path= "/userLogin " name= "userLoginForm " input= "/userLogin.jsp " scope= "request "
* @struts.action-forward name= "success " path= "/userLoginSuccess.jsp "
* @struts.action-forward name= "failure " path= "/userLogin.jsp "
*/
public class UserLoginAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserLoginForm userLoginForm = (UserLoginForm) form;// TODO Auto-generated method stub


if(userLoginForm.getUserName().equals( "zq ")&&
userLoginForm.getPassword().equals( "110 ")){
request.setAttribute( " ", userLoginForm.getUserName());
return mapping.findForward( "success ");
}
return mapping.findForward( "failure ");
}
}
-----------------------------------
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;

import org.apache.struts.action.ActionForm;

/**
* MyEclipse Struts
* Creation date: 02-11-2007
*
* XDoclet definition:
* @struts.form name= "userLoginForm "
*/
public class UserLoginForm extends ActionForm {
/*
* Generated fields
*/

/** password property */
private String password;

/** userName property */
private String userName;

/*
* Generated Methods
*/

/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}

/**
* Set the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}

/**
* Returns the userName.
* @return String
*/
public String getUserName() {
return userName;
}

/**
* Set the userName.
* @param userName The userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
}
-----------------------

[解决办法]
楼上的已经非常正确了呃
[解决办法]
多写写就会了。
[解决办法]
你的action继承一个BASEACTION,计数问题放在里面处理,然后再塞到application

在jsp用标签把application中的内容读出来就可以了
[解决办法]
有action中如何访问像session、application啊?

读书人网 >Java Web开发

热点推荐