//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=" <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"?> <struts-config> //UserAction.java package com.abin.struts1.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import com.abin.struts1.form.UserForm; public class UserAction extends DispatchAction { public ActionForward user(ActionMapping mapping, ActionForm form, } //UserForm.java package com.abin.struts1.form; import org.apache.struts.action.ActionForm; public class UserForm extends ActionForm{ //index.jsp這個放置在WebContent下面(我這里使用的eclipse,如果是Myeclipse就直接放置在WebRoot下面) <%@ page language="java" contentType="text/html; charset=utf-8" //add.jsp這個放置在WebContent下面(我這里使用的eclipse,如果是Myeclipse就直接放置在WebRoot下面) <%@ page language="java" contentType="text/html; charset=utf-8"
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "
<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>
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
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");
}
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;
}
}
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<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>
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<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>