初学struts碰到的问题

初次使用struts,碰到了一些小问题。首先先说一下怎样为web应用增加struts支持。

1、就是要下载Struts2了,下载好了就把核心类库(jar包)增加到web应用中,即将lib下的struts2-core-2.1.8.jar、xwork-2.1.6.jar、freemarker-2.3.16.jar、ognl-2.7.3.jar等复制到web应用(就是你的项目啦)的lib路径下。

2、在web.xml里配置struts的核心filter了

这么简单就ok了,下面就写了个例子来测试一下

1、先写一个登录页面login.jsp,提交表单给指定的action,其中s是标签,body里是用struts2标签库定义的一个表单和三个简单的表单域

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <s:form action="login" method="post">
      <s:textfield name="username" key="user"/>
      <s:textfield name="password" key="pass"/>
      <s:submit key="login"/>
    </s:form>
  </body>
</html>

 2、login.jsp页面指定了表单的action为login,因此要定义一个struts2 的action,action通常继承ActionSupport基类,下面是lee.LoginAction.java

package lee;

import com.opensymphony.xwork2.ActionContext;

public class LoginAction {
    private String username;
    private String password;
    
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsername() {
		return username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return password;
	}
    
	public String execute() throws Exception{
		if(getUsername().equals("wang")&&getPassword().equals("123")){
			ActionContext.getContext().getSession().put("user", getUsername());
			return "success";
		}
		else {
			return "error";
		}
	}
    
}

 代码里定义封装请求参数的uername和password属性,其中execute就是处理客户请求的方法,即只要登录名为wang,密码为123就登陆成功,返回success,否则返回error

3、写好了1和2,怎么样让其对应起来呢,在没有学spring管理struts的action之前,是在struts的配置文件struts.xml

里配置的,下面是配置好的struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

	<!-- 是否显示详细错误信息 -->
	<constant name="struts.devMode" value="true" />
	<!-- 国际化资源文件名称 -->
	<constant name="struts.custom.i18n.resources" value="i18n" />
	<!-- 是否自动加载国际化资源文件  -->
	<constant name="struts.i18n.reload" value="false" />
    <!-- 所有的Action都应该定义在package下 -->
    <package name="lee" extends="struts-default">
      <action name="login" class="lee.LoginAction">
      <!-- 定义3个逻辑视图和物理资源之间的映射 -->
      <result name="error">/index.jsp</result>
      <result name="success">/success.jsp</result>
      </action>
      </package>
</struts>

 这样,这个测试就写好了,部署后,浏览login.jsp页面(http://localhost:8880/strutsdemo/login.jsp)

下面是测试当中出现的一些问题

1、Unable to load configuration. - bean - jar:file:

这个是struts有个jar包没放进去,就是commons-logging-1.1.1.jar,导进去就ok了

2、

There is no Action mapped for namespace / and action name login. - [unknown location]

有经验的就知道,这个是因为没有找到映射的action 空间名或者 没有找到匹配的 action名。 出现这样的问题,一般是因为没有加载 配置文件引起的。

果然。在布署的工程目录classes下,没有找到 struts.xml。

一般而言,对于没有经验的程序员来说,习惯用于集成IDE。以为在IDE里面的文件,理所当然的会把各种文件布署在对应的目录下。但往往有些时候,它也偶尔会发发脾气,不听使唤的。^-^ 在其他的时候,也会因为使用IDE造成这样或者那样的错误。所以还是要自己明白,整个运行流程。然后一步一步的去排查。

猜你喜欢

转载自tangyuan1314.iteye.com/blog/1470889
今日推荐