基於Struts的第一個專案

yeluowuhen發表於2009-08-12
好久沒來這,最近在做實習,遇到好多問題應該貼在這的,失誤!
晚上在看Struts的視訊,尚學堂的入門教程,對我這個初入門者來說這樣手把手教挺好的。
最簡單的例子:登入管理,其中
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登入管理</title>
</head>
<body>
<h1>使用者登入</h1>
<hr>
<form action="login.do" method="post">
使用者名稱:<input type="text" name="username"><br>
<br>
密 碼:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

成功後,轉入login_success.jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GTF-8">
<title>登入成功</title>
</head>
<body>
<%=request.getAttribute("username") %>,登入成功;
</body>
</html>

失敗後,轉入login_error.jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GTF-8">
<title>登入失敗</title>
</head>
<body>
登入失敗;
</body>
</html>

Servlet層由Action來完成,LoginAction來處理從ActionForm中取得的引數,之後決定跳轉結果。
package com.wxn.structs;

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;

public class LoginAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
// TODO Auto-generated method stub
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();

if("admin".equals(username)&&"admin".equals(password)){
req.setAttribute("username", username);
return mapping.findForward("success");
}
else{
return mapping.findForward("error");
}
}

}

LoginActionForm得到頁面的引數。
package com.wxn.structs;

import org.apache.struts.action.ActionForm;

public class LoginActionForm extends ActionForm {

private String username;
private String password;

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

出現錯誤如下:
嚴重: Servlet.service() for servlet action threw exception
org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.
at org.apache.struts.chain.commands.AbstractSelectAction.execute(AbstractSelectAction.java:71)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
檢查很久,編碼肯定沒錯,估計是配置的問題,果然,在struts-config.xml中,action的path屬性配錯,無.do,如下:
struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="loginForm" type="com.wxn.structs.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="com.wxn.structs.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"></forward>
<forward name="error" path="/login_error.jsp"></forward>
</action>
</action-mappings>
</struts-config>

期間也出現過<struts-config>不符合規定的問題,是<form-beans>少寫了,記住:這裡是複數。
同是,貼出web.xml吧:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


</web-app>

終於要的效果出來了,一個晚上的時間,還是這樣手把手的,我要加油呀!!

相關文章