好久没来这,最近在做实习,遇到好多问题应该贴在这的,失误!
晚上在看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>
终于要的效果出来了,一个晚上的时间,还是这样手把手的,我要加油呀!!
分享到:
相关推荐
struts2+hibernate一周小项目总结
为了寝室里交大作业,用struts2写的一个电子商城系统。包含完整的数据库创建文件,在根项目的sql文件夹里,还有初步的测试数据。整个项目分包明确,每个功能模块一目了然。根据网上的资源实现了一个登陆时邮箱验证的...
Java Web整合开发实战--基于Struts 2+Hibernate+Spring.pdf 1章 Web的工作机制 2章 搭建Java Web开发环境 3章 JSP及其相关技术 2篇 表现层框架Struts技术 4章 Struts快速上手 5章 解密Struts之核心文件 6章 ...
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
a) Struts2是以web work优秀的设计思想为核心,吸收了struts1的部分优点,建立了一个基于webwork和struts1的MVC框架。 二、 优点: a) 结构清晰,使开发者只关注业务逻辑实现即可。 b) 提供了丰富的标签,大大提高了...
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
ssh项目实例-企业信息管理系统项目jar包第一部分,项目图片以及部分说明可查看空间http://user.qzone.qq.com/260316820/blog/1300093298 有指教或者疑问可空间留言,希望我们一起进步:)
这是一个实际使用中的项目,可访问 http://cdbke.cuit.edu.cn 可查看效果及功能,后台需要登录后才能使用(出于安全性考虑这里就不给大家介绍了,另外还有一个struts1版本的,如有需要可以与我联系)。 此项目整合...
基于Struts2框架的第一个web工程。可了解Struts框架的结构。可直接部署在Tomcat上运行
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会 Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
笔者刚刚入门学习了使用EclipseEE平台开发基于struts1的Web JSP程序,正式调试完成了第一个struts1程序,希望可以对需要的朋友有所帮助
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会 Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
本书第1章到第3章为入门篇,通过两个Struts应用实例,引导读者把握设计、开发和部署Struts应用的整体流程,充分体会 Struts框架在开放大型、可扩展的Web应用方面发挥的优势。第4章到第7章深入探讨了Struts框架的核心...
pdf已分割,每一个pdf为一个章节,想要阅读指定章节只需打开该章节的pdf文档即可,再也不用使用鼠标拖半天了。
第一部分 Strut 2:一个全新的框架 第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 ...
对于系统的安全性的第一关,就是用户想要进入系统,必须通过登录窗口,输入自己的登录信息才可以进行登录,用户输入的信息准确无误后才可以进入到操作系统界面,进行功能模块的相对应操作,如果用户输入的信息不正确...
MySQL 是一款广受欢迎的开源关系型数据库管理系统(RDBMS),由瑞典MySQL AB公司开发,现隶属于美国甲骨文公司(Oracle)。自1998年首次发布以来,MySQL以其卓越的性能、可靠性和可扩展性,成为全球范围内Web应用...
在2007年上半年,Struts 2终于在众多开发者期盼中,发布了第一个稳定版本:2.0.6。 为了让众多Struts学习、工作者快速掌握Struts 2的开发,笔者精心编著了本书。 本书按Struts 2的架构体系,细致地介绍了Struts 2...
以国内使用最广泛的Struts为例,其新版本Struts2与前一版Struts1.x已经有了天壤之别。Hibernate也从2.0升级为3.X,近期Spring也最新推出了2.5版。读者如果再参考以前的版本书籍学习,无异于南辕北辙,而《JavaWeb...