学习笔记-----------struts2请求参数接收

一.

1.设置action


package cn.itcast.action;
import cn.itcast.bean.Person;
public class HelloWorldAction {//?id=23&name=xxx
	private Integer id;
	private String name;
	private Person person;
	
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String addUI(){
		return "success";
	}
	public String execute() throws Exception{
		return "success";
	}
}


get()函数用于接收表单数据,页面想获得数据需要set()函数

 


 

 

2.struts2配置struts.xml,struts.xml建在src目录下。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.action.extension" value="do,action"/>
	<package name="employee" namespace="/control/employee" extends="struts-default">
		<action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
			<result name="success">/WEB-INF/page/message.jsp</result>
		</action>
	</package>
</struts>


复合类型如Person,在类中必须有默认无参构造方法。

 


 

 

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
	
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


 


 

Person类代码如下:

package cn.itcast.bean;
public class Person {
	private String name;
	private Integer id;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
}


 


 

 

4.message.jsp,message.jsp放置路径在struts.xml中找到。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'message.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
   id=${person.id }<br/>
   name=${person.name }
  </body>
</html>


 


 

5.访问路径http://localhost:8080/struts2/control/employee/list_execute.action?id=123123&name=xxx

 


 

6.表单提交

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
   <form action="<%=request.getContextPath()%>/control/employee/list_execute.action" method="post">
   		id:<input type="text" name="person.id"><br/>
   		name:<input type="text" name="person.name"><br/>
   		<input type="submit" value="发送"/>
   </form>
  </body>
</html>


 


 

 

二.中文请求参数乱码问题,提交用post不会出现乱码。

1.

 


 

 

 

 

 所需jar包,这些包在这个例子有的没有用到。

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

g710710

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值