我的欢迎页面
welcome.jsp
列于web.xml
文件
welcome.jsp
包含一个简单的表单,用于提交到由struts-config.xml
文件映射的操作
当表单提交到浏览器显示空白页面时,所有映射都是正确的?我不知道是什么问题。
地址栏显示正确的 URL,该 URL 执行了正确的操作,但仍然显示空白页面。
我在片段中提供我的
welcome.jsp
、 struts-config.xml
文件、操作
struts-config.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="login1" type="aditya.bean.LoginBean"/>
</form-beans>
<!-- <global-forwards>
</global-forwards>-->
<!-- Action Mappings -->
<action-mappings>
<action path="/login"
type="aditya.action.LoginAction"
name="login1"
parameter="method"
/>
<forward name="loginadmin" path="/jsp/adminwel.jsp" />
<forward name="loginuser" path="tuser"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<!-- <message-resources parameter="ApplicationResources" null="false" /> -->
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-def.xml"/>
<set-property property="moduleAware" value="true"/>
</plug-in>
</struts-config>
welcome.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<h:form action="login.do" method="post">
<table border="5" bgcolor="cyan" align="center">
<tr><td>USER ID</td><td><h:text property="uid"/></td></tr>
<tr><td>PASSWORD</td><td><h:password property="pass"/></td></tr>
<tr><td><h:submit property="method" value="login"/></td></tr>
</table>
</h:form>
</body>
</html>
LoginAction
:
package aditya.action;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import aditya.bean.LoginBean;
public class LoginAction extends DispatchAction {
public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res)throws ServletException
{
LoginBean loginBean=(LoginBean)form;
System.out.println("in action-");
if(loginBean.getUid().equalsIgnoreCase("admin")&&loginBean.getPass().equalsIgnoreCase("admin"))
{
return mapping.findForward("loginadmin");
}
else
{
return mapping.findForward("loginuser");
}
}
}
为什么浏览器显示空白网页?
使用的IDE --> Eclipse
显示空白页面是因为操作类没有返回到浏览器的结果。在 login()
操作中,代码应返回
ActionForward
实例。但它会返回null
。定义全局转发:
<global-forwards>
<forward name="error" path="/error.jsp"/>
</global-forwards>
动作代码:
ActionForward result;
if(loginBean.getUid().equalsIgnoreCase("admin")&&loginBean.getPass().equalsIgnoreCase("admin"))
{
result = mapping.findForward("loginadmin");
if (result == null)
return mapping.findForward("error");
}
else {
result = mapping.findForward("loginuser");
if (result == null)
return mapping.findForward("error");
}
return result;