求 struts 页面传值问题 实例.
在Action中进行查询数据库 不知道如何才能传值到JSP页面中显示出来
谁有这方面的例子可以拿出来分享下 要求用到 httpServletRequest.getSession().setAttribute(); 例子要完整 可以直接调试查看 谢谢!@~~~
在Action中进行查询数据库 不知道如何才能传值到JSP页面中显示出来
谁有这方面的例子可以拿出来分享下 要求用到 httpServletRequest.getSession().setAttribute(); 例子要完整 可以直接调试查看 谢谢!@~~~
解决方案 »
- java 读取Excel异常,欢迎解读,高分答谢
- 请前辈推荐一本JAVA书
- java与mysql的问题
- 用JAVA怎么将SQL SERVER 数据库里面的数据用曲线表示出来,类似于在X、Y坐标中,并显示在网页中? [Java][J2SE / 扩展类]
- 把文章数据库中调出显示在网页上该怎么才能换行呀?头都大了
- 怎么在jsp页面上点击按钮就可生成pdf啊
- 还是hibernate的问题,我的有个表挂起了!!!!
- 急!!! 提示:java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 无效的游标状态
- 如何禁止IE的回退历史页功能
- jsp 数据库连接
- 急!急!那位大侠帮我解读下面一段jsp代码!
- JSP输出带有html标签的字符串,怎么才能显示出html标签。
你那就不叫struts了
struts都是用標簽和formbean來顯示jsp的
getSession().setAttribute()的話,有可能會出錯的
什么不同的人用到同一個session,或者別人推出的時候把你的seesion里的數據也清空了什么的亂七八糟的問題
===============================================
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="cn.com.tarena.domain.*"%>
<%@ page import="java.util.*"%>
<%
String productId=request.getParameter("productId");
Map<String,Product> productMap=(Map<String,Product>)application.getAttribute("productMap");
//
Product product=productMap.get(productId);
ShopCart shopCart=(ShopCart)session.getAttribute("shopCart");
shopCart.removeProduct(product);
if(shopCart.getOrderLines().size()>0){
response.sendRedirect(request.getContextPath()+"/shop/showShopCart.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
%>
這只是個例子。
Servlet:(Action)package servlet;import imgvalidate.Codejpg;import java.io.IOException;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import dao.ShopUserDao;
import dto.ShopUser;public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void destroy() {
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Codejpg c = new Codejpg();
String username = request.getParameter("username");
String password = request.getParameter("password");
String img = request.getParameter("img");
String filename = request.getParameter("filename");
String filevalue = request.getParameter("value");
if(filevalue.equals(img)){
c.deleteImage(filename, request);
int flag = 8;
HttpSession session = request.getSession();
ShopUserDao sud = new ShopUserDao();
if (sud.booleanLogin(username, password)) {
ArrayList list = sud.Login(username, password);
for (int i = 0; i < list.size(); i++) {
ShopUser su = (ShopUser) list.get(i);
if (flag == su.getFlag()) {
session.setAttribute("role", "manager");
response.sendRedirect("descmanager.jsp");
} else {
session.setAttribute("role", "cashier");
response.sendRedirect("desccashier.jsp");
}
}
} else {
response.sendRedirect("index.html");
}
}else{
c.deleteImage(filename, request);
response.sendRedirect("index.html");
System.out.println("验证码错误!");
}
} public void init() throws ServletException {
}}
action里面:
request.setAttribute("results",queryResults);
jsp页面:
<logic:iterator id="aBean" name="results">
<bean:write name="aBean" property="XXX"/>
</logic:iterator>
<%
String role = session.getAttribute("role").toString();
if("cashier".equals(role)){
response.sendRedirect("index.jsp");
return;
}else{
%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for loginForm form</title>
</head>
<body>
<H1>LOGIN SUCCESSED!!!</H1>
欢迎 <h1>${id}</h1> 登陆!!!
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for loginForm form</title>
</head>
<body>
<html:errors />
<html:form action="/login">
id : <html:text property="id"/><br/>
password : <html:password property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for loginForm form</title>
</head>
<body>
<H1>LOGIN FAILED ,MAY BE CAUSED BY:</H1>
<html:errors/>
</body>
</html>
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xslpackage com.globalsources.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;import com.globalsources.struts.form.LoginForm;/**
* MyEclipse Struts
* Creation date: 04-16-2008
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
LoginForm loginForm = (LoginForm) form;
String id = loginForm.getId();
String password = loginForm.getPassword();
HttpSession session = request.getSession();
if("huang".equals(id)&&"dilaomimi".equals(password))
{
System.out.println("ID :"+id);
System.out.println("PASSWORD :"+password);
session.setAttribute("id",id);
session.setAttribute("password",password);
return mapping.findForward("suc");
}
else
{
ActionErrors errors = new ActionErrors();
errors.add("name",new ActionMessage("wrongNameOrPassword"));
super.saveErrors(request,errors);
return mapping.findForward("fail");
}
}}
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xslpackage com.globalsources.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;/**
* MyEclipse Struts
* Creation date: 04-16-2008
*
* XDoclet definition:
* @struts.form name="loginForm"
*/
public class LoginForm extends ActionForm { // --------------------------------------------------------- Instance Variables /** password property */
private String password; /** id property */
private String id; // --------------------------------------------------------- Methods /**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = null;
if(this.id==null||"".equals(this.id))
{
errors = new ActionErrors();
errors.add("name",new ActionMessage("nameIsNull"));
}
if(this.password==null||"".equals(this.password))
{
errors = new ActionErrors();
errors.add("password",new ActionMessage("passwordIsNull"));
}
return errors;
} /**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub
} /**
* 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 id.
* @return String
*/
public String getId() {
return id;
} /**
* Set the id.
* @param id The id to set
*/
public void setId(String id) {
this.id = id;
}}
passwordIsNull=<li>\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
wrongNameOrPassword=<li>\u9519\u8bef\u7684\u7528\u6237\u540d\u6216\u5bc6\u7801\uff01\uff01\uff01
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.globalsources.struts.form.LoginForm" /> </form-beans>
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.globalsources.struts.action.LoginAction">
<forward name="suc" path="/login_suc.jsp" />
<forward name="fail" path="/login_fail.jsp" />
</action>
</action-mappings> <message-resources parameter="com.globalsources.struts.ApplicationResources" />
</struts-config>
login_success.jsp
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;// TODO Auto-generated method stub
String uname=(String) loginForm.get("username");
String passwd=(String) loginForm.get("password"); UserinfoDAO udao=new UserinfoDAO();
Userinfo user=new Userinfo();
ActionMessages errors=new ActionMessages(); user.setUsername(uname);
user.setPassword(passwd);
if(udao.checkLogin(user))
{
request.getSession().setAttribute("user", uname);
return(mapping.findForward("tomain"));
}
else
{
errors.add("error",new ActionMessage("logon.jsp.error"));
saveErrors(request,errors);
return(new ActionForward(mapping.getInput()));
}
}
}
jsp页面中:……
<html:form action="/login" method="post" focus="login" onsubmit="return validateLoginForm(this)">……//省略 <tr>
<td width="57" height="35">用户名:</td>
<td colspan="3"><html:text property="username" /><html:errors property="message"/></td>
</tr>
<tr>
<td height="34">密 码:</td>
<td colspan="3"><html:password property="password" /><html:errors property="message"/></td>
</tr>
……省略的html<html:link page="/main.jsp">//页面连接……省略的html
</html:form>
<html:javascript formName="loginForm"/>
list.add("Hello World");
request.setAttribute("hello",list);页面:
${hello}
struts2 。
不要学1版本了
request.setAttribute("wori",list);mapping到jspJsp:List list=(list)request.getAttribute("wori");把list打印出来完。