//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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts</display-name>
<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/classes/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<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>/WEB-INF/struts-html</taglib-uri>
<taglib-location>WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-bean</taglib-uri>
<taglib-location>WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic</taglib-uri>
<taglib-location>WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</jsp-config>
-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
//struts-config.xml这个放置在src目录下面
<?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="userForm" type="com.abin.struts1.form.UserForm" ></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
path="/userAction"
type="com.abin.struts1.action.UserAction"
name="userForm"
parameter="method"
scope="request"
validate="true"
input="/add.jsp"
>
<forward name="add" path="/index.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
//UserAction.java
package com.abin.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.abin.struts1.form.UserForm;
public class UserAction extends DispatchAction {
public ActionForward user(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse arg3) throws Exception {
UserForm userForm = (UserForm) form;
String un = userForm.getUsername();
String pw = userForm.getPassword();
System.out.println("username="+un);
System.out.println("password="+pw);
return mapping.findForward("add");
}
}
//UserForm.java
package com.abin.struts1.form;
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm{
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//index.jsp这个放置在WebContent下面(我这里使用的eclipse,如果是Myeclipse就直接放置在WebRoot下面)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>struts1.3 test</title>
</head>
<body>
<a href="add.jsp" target="_blank" >新增</a>
</body>
</html>
//add.jsp这个放置在WebContent下面(我这里使用的eclipse,如果是Myeclipse就直接放置在WebRoot下面)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>struts1.3 test</title>
</head>
<body>
<form action="userAction.do" method="post">
<input type="hidden" name="method" value="user" />
用户名:<br/>
<input type="text" name="username" id="username" /><br/>
密码:<br/>
<input type="text" name="password" id="password" /><br/>
<input type="submit" value="提交" />
<input type="reset" value="重置"/>
</form>
</body>
</html>
struts1.3 入门级例子
最新推荐文章于 2024-10-30 18:07:32 发布