struts1.3环境搭建示例

1.新建web工程,在WEB-INF/lib下放入struts1.3官网上下载的的所有library。

2.解压struts-taglib-1.3.10.jar,将其中的标签文件(*.tld)放入WEB-INF下

3.在web.xml上配置ActionServlet和标签库

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstStruts</display-name>
  
  <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <jsp-config>
      <taglib>
          <taglib-uri>bean_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-bean.tld</taglib-location>
      </taglib>
      <taglib>
          <taglib-uri>logic_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-logic.tld</taglib-location>
      </taglib>
      <taglib>
          <taglib-uri>html_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-html.tld</taglib-location>
      </taglib>
  </jsp-config>
</web-app>
4.在WEB-INF下新建并配置struts.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-beans>               <!-- ActionForm -->
    <global-exceptions></global-exceptions>  <!-- 全局跳转 -->
    <global-forwards></global-forwards>      <!-- 全局异常 -->
    <action-mappings></action-mappings>      <!-- Action -->
    <message-resources parameter=""></message-resources>  <!-- 配置资源文件路径 -->
</struts-config>

下面用一个例子说明:

用户注册页面register.jsp,密码2次输入相同并提交表单后,转入success.jsp,否则进入failure.jsp

工程目录:


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstStruts</display-name>
  
  <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <jsp-config>
      <taglib>
          <taglib-uri>bean_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-bean.tld</taglib-location>
      </taglib>
      <taglib>
          <taglib-uri>logic_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-logic.tld</taglib-location>
      </taglib>
      <taglib>
          <taglib-uri>html_uri</taglib-uri>
          <taglib-location>/WEB-INF/tags/struts-html.tld</taglib-location>
      </taglib>
  </jsp-config>
</web-app>

register.jsp:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="bean_uri" prefix="bean"%>
<%@ taglib uri="html_uri" prefix="html"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
  <html:form action="/register.do">
      用户名:<html:text property="username"></html:text><br/>
      密码:<html:text property="password1"></html:text><br/>
      重复密码:<html:text property="password2"></html:text><br/>
      <html:submit value="注册"></html:submit>
  </html:form>
</body>
</html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>注册成功</title>
</head>
<body>
<p>注册成功</p><br/>
</body>
</html>
failure.jsp:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>注册失败</title>
</head>
<body>
<p>注册失败</p><br/>
</body>
</html>
RegisterForm.java:表单提交后生成实例,表单内的对应值被注入

package com.test;

import org.apache.struts.action.ActionForm;

public class RegisterForm extends ActionForm {
	private String password2;
	private String password1;
	private String username;
	
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getPassword1() {
		return password1;
	}
	public void setPassword1(String password1) {
		this.password1 = password1;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
}
RegisterAction.java:

package com.test;

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 RegisterAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//将form转换成RegisterForm
		RegisterForm rf = (RegisterForm) form;
		String username = rf.getUsername();
		String password1 = rf.getPassword1();
		String password2 = rf.getPassword2();
		ActionForward forward = new ActionForward();
		if (password1 != null && password2 != null
				&& password1.equals(password2)) {
			forward = mapping.findForward("success");
		} else {
			forward = mapping.findForward("failure");
		}
		return forward;
	}
	
}
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="RegisterForm" type="com.test.RegisterForm" ></form-bean>
    </form-beans>
    <action-mappings>
        <action path="/register" type="com.test.RegisterAction"
            name="RegisterForm" scope="request" input="/register.jsp">
            <forward name="success" path="/success.jsp"></forward>
            <forward name="failure" path="/failure.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值