Struts1基本配置

以下记录Struts1基本配置。

一、下载Struts1.3

链接如下:http://struts.apache.org/download.cgi#struts1310

二、拷贝lib下面的所有包到项目WEB-INF\LIB下。

三、修改web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	<display-name>TestStruts</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/struts-config.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<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>

 

四、在WEB-INF下新建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="logonForm" type="com.lwf.struts.form.UserForm"></form-bean>
    </form-beans>

    <global-exceptions>
        <!-- sample exception handler
        <exception
            key="expired.password"
            type="app.ExpiredPasswordException"
            path="/changePassword.jsp"/>
        end sample -->
    </global-exceptions>

    <global-forwards>
        <!-- Default forward to "Welcome" action -->
        <!-- Demonstrates using index.jsp to forward -->
        <forward
            name="welcome"
            path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
         <action 
         	path="/logon"
         	type="com.lwf.struts.action.LogonAction"
         	name="logonForm"
         	input="/input.jsp"
         	scope="session"
         	validate="false"
         >
         <forward name="resultForward" path="/result.jsp"></forward>
         </action>
    </action-mappings>


<!-- ======================================== Message Resources Definitions -->

    <message-resources parameter="MessageResources" />


<!-- =============================================== Plug Ins Configuration -->

  <!-- ======================================================= Tiles plugin -->
  <!--
     This plugin initialize Tiles definition factory. This later can takes some
	 parameters explained here after. The plugin first read parameters from
	 web.xml, thenoverload them with parameters defined here. All parameters
	 are optional.
     The plugin should be declared in each struts-config file.
       - definitions-config: (optional)
            Specify configuration file names. There can be several comma
		    separated file names (default: ?? )
       - moduleAware: (optional - struts1.1)
            Specify if the Tiles definition factory is module aware. If true
            (default), there will be one factory for each Struts module.
			If false, there will be one common factory for all module. In this
            later case, it is still needed to declare one plugin per module.
            The factory will be initialized with parameters found in the first
            initialized plugin (generally the one associated with the default
            module).
			  true : One factory per module. (default)
			  false : one single shared factory for all modules
	   - definitions-parser-validate: (optional)
	        Specify if xml parser should validate the Tiles configuration file.
			  true : validate. DTD should be specified in file header (default)
			  false : no validation

	  Paths found in Tiles definitions are relative to the main context.

      To use this plugin, download and add the Tiles jar to your WEB-INF/lib
      directory then uncomment the plugin definition below.

    <plug-in className="org.apache.struts.tiles.TilesPlugin" >

      <set-property property="definitions-config"
                       value="/WEB-INF/tiles-defs.xml" />
      <set-property property="moduleAware" value="true" />
    </plug-in>
  -->  


  <!-- =================================================== Validator plugin 

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/org/apache/struts/validator/validator-rules.xml,
               /WEB-INF/validation.xml"/>
  </plug-in>
-->
</struts-config>

 

五、新建UserForm类文件

package com.lwf.struts.form;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm{

	private String userName;
	private String password;
	
	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;
	}

	
}

 

 

新建LogonAction文件

package com.lwf.struts.action;

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;

import com.lwf.struts.form.UserForm;

public class LogonAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		UserForm userForm = (UserForm)form;
		String name = userForm.getUserName();
		String pwd = userForm.getPassword();
		
		
		return mapping.findForward("resultForward");
	}

}

 

六、新建所需的index.jsp, result.jsp,logon.jsp三个文件

index.jsp如下

<%@ 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>Insert title here</title>
</head>
<body>
<%
	out.print(new java.util.Date());
%>
<table>
	<tr><td><a href="./Logon.jsp">Logon</a></td></tr>
</table>
</body>
</html>

 

logon.jsp如下。

<%@ 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>Insert title here</title>
</head>
<body>
<form action="logon.do">
	UserName:<input type="text" name="userName"></input>
	Password:<input type="text" name="password"></input>
	<input type="submit" name="submit"></input>
</form>
</body>
</html>

 

result.jsp如下:

<%@ 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>Insert title here</title>
</head>
<body>
result
</body>
</html>

 

运行程序,最终结果返回到result.jsp,显示result.

 

 

附件为代码:

你可能感兴趣的:(apache,jsp,Web,struts,servlet)