`
yeluowuhen
  • 浏览: 156871 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

基于Struts的第一个项目

阅读更多
   好久没来这,最近在做实习,遇到好多问题应该贴在这的,失误!
   晚上在看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>

   终于要的效果出来了,一个晚上的时间,还是这样手把手的,我要加油呀!!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics