Developing a Web Application Using Struts

Developing a Web Application Using Struts

1.在/WEB-INF/web.xml文件中加入如下代码:


web.xml
<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

2.在/WEB-INF/中添加struts-config.xml文件

struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    
<action-mappings>
        
        
<action path="/add_DVD"
                type
="controller.AddDVDAction">
            
<forward name="success" path="/success.view" />
            
<forward name="error" path="/add_DVD.view" /> 
        
</action>
        
        
<action path="/set_prefs"
                type
="controller.SetPreferencesAction">
            
<forward name="success" path="/index.html" />
        
</action>
        
    
</action-mappings>
</struts-config

3.根据需求添加相应的Action

Action
package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
// Struts classes
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

public class SetPreferencesAction extends Action {
    
    
public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
        
          
//
    }

4.修改相应struts-config.xml中配置。

你可能感兴趣的:(Developing a Web Application Using Struts)