文章目录
1.web.xml中struts配置
在web.xml中配置struts,就是配置一个servlet,主要的struts的配置是在struts-confing.xml中配置的
图片说明
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<display-name>zzzmvc</display-name>
<welcome-file-list>
<welcome-file>index.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>
<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>
</web-app>
2.struts-config.xml配置
图片说明
<?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>
<action-mappings>
<action path="/hello" type="action.HelloAction">
<forward name="success" path="/struts1Test/hello.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="structs.ApplicationResources"></message-resources>
</struts-config>
3.Action
需要写一个实现类是继承于Action 注意是apache包下面的
重写里面的execute()方法
注意:重写的是参数为httpServletRequest的方法 而不是ServletRequest
package action;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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;
public class HelloAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//得到请求参数
String name = request.getParameter("name");
System.out.println(name);
//request传递参数
request.setAttribute("name", name);
//这里的success就是struts-config配置中的success
//在这里表示 返回的是在配置文件中success对应的页面
return mapping.findForward("success");
}
}
4.代码
hello.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>简单的struts </title>
</head>
<body>
<h1>hello 案例</h1>
<hr>
<form action="${pageContext.request.contextPath }/hello.do" >
姓名:<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
<br/>
${name }
</body>
</html>
HelloAction
package action;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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;
public class HelloAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
System.out.println(name);
request.setAttribute("name", name);
return mapping.findForward("success");
}
}
web.xml 和 struts-config.xml的配置上面都有
到这里一个简单的struts成功了,一个简单的mvc实现了