eclipce搭建struts1.2

demo资源:https://download.csdn.net/download/qq3892997/10605201

1、新建项目

在Eclipse中新建一个Dynamic Web Project,取名为struts。

2、导入Struts1.2所需的Jar包

将Struts1.2Jar包拷贝到项目目录下WebContent/WEB-INF/lib文件夹中,右击鼠标buildpath--add...。

jar下载地址:https://download.csdn.net/download/qq3892997/10605213

3、配置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" 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>struts</display-name>
  <welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
  </welcome-file-list>
  
   <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>
</web-app>

这个Servlet是Struts的入口,任何以*.do结尾的请求都会被映射到Struts的Servlet上,该Servlet会根据struts-config.xml的配置,将请求分配到指定的Action上。

4、创建Action和Form文件

在src文件夹下新建action和form包,在其中分别新建HelloAction.java和HelloForm.java

HelloAction.java代码如下:

package struts.action;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import struts.form.HelloForm;
public class HelloAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		HelloForm helloForm = (HelloForm)form;
		if(helloForm.getName() == null || helloForm.getName().trim().length() == 0) {
			return mapping.getInputForward();
//if表单Name为空则跳转到xml中配置的“input="/hello.jsp"”的hello.jsp页面
		}
		return mapping.findForward("success");
	//否则跳转至xml中配置<forward name="success" path="/WEB-INF/success.jsp" />的success.jsp页面
}

HelloForm.java代码如下:

package struts.form;

import org.apache.struts.action.*;

public class HelloForm extends ActionForm {
	private String name;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
}

5、将Action和Form文件配置到struts-config.xml

struts-config.xml配置代码如下:(这里面只配置了form和action,其他的暂且空着,实际项目需配置其他项)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
 
<struts-config>
 
  <form-beans>
    <form-bean name="helloForm" type="struts.form.HelloForm" />
  </form-beans>
 
  <action-mappings>
  	<action attribute="helloForm" input="/ hello.jsp" name="helloForm"
  			path="/hello" scope="request" type="struts.action.HelloAction">
  	  <forward name="success" path="/WEB-INF/success.jsp" />
  	</action>
  </action-mappings>
 
</struts-config>

6、添加JSP页面

在WebContent下与WEB-INF同级目录新建hello.jsp和WEB-INF同级目录下新建success.jsp,hello.jsp建立在web-inf是为了访问项目名称时能通过web.xml配置的默认页面直接访问到hello.jsp

hello.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
	<head>
		<title>Hello JSP</title>
	</head>
	<body>
		<html:form action="/hello">
			姓名: <html:text property="name" />
			<html:submit value="提交"/>
		</html:form>
	</body>
</html>

success.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
	<head>
		<title>Success JSP</title>
	</head>
	<body>
		<h2>Hello, ${ helloForm.name }. Welcome to Struts World!</h2>
	</body>

</html>

7、运行

到现在为止一个简单的Struts项目就搭建好了,添加项目到tomcat,启动Tomcat服务器。

在浏览器栏输入http://localhost:8080/Struts,展示hello.jsp页面。

在输入栏输入555,点击提交,展示success.jsp页面。

参考链接:https://blog.csdn.net/liulisishui/article/details/50260483

猜你喜欢

转载自blog.csdn.net/qq3892997/article/details/81702385
今日推荐