struts1 入门案例——显示名字

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实现了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值