标题:
struts2中jsp页面传值到action方式总结(1)
[打印本页]
作者:
look_w
时间:
2019-4-20 13:29
标题:
struts2中jsp页面传值到action方式总结(1)
jsp页面跟通常的html传值一样 通常有两种方式
1.form表单传值
2.url方式传值
通常后台接受前端传值 都是用的 request.getParameter("username")
但是在struts框架下 可以用set方法让action自动接收 也更方便
所以 action接收值也有两种方式
1.request.getParameter("username")
2.set
下面记录多种组合方式,大家可以根据相应的情况选择使用:
方式一 jsp中form传值username,password, action中set方法接收
<%@taglib prefix="s" uri="/struts-tags" %>
<form action="getIp/login" method="post" name="form1">
用户名:
<s:textfield name="username" />
<br />
密 码:
<s:password name="password" />
<br />
<s:submit value="提交" />
</form>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>login page</title>
</head>
<body>
<form action="getIp/login" method="post" name="form1">
用户名:
<s:textfield name="username" />
<br />
密 码:
<s:password name="password" />
<br />
<s:submit value="提交" />
</form>
</body>
</html>
loginAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
public class loginAction extends ActionSupport {
/**
* 登录
*/
private static final long serialVersionUID = -6797327769546503535L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
System.out.println("username:" + username);
System.out.println("password:" + password);
return SUCCESS;
}
}
spring文件
action.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/s ... ing-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<!-- <bean id="testAction" class="action.TestAction">
</bean> -->
<bean id="login" class="action.loginAction">
</bean>
</beans>
struts文件
action.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="getIp" extends="json-default" namespace="/getIp">
<!-- <action name="testJson" class="testAction">
<result type="json"></result>
</action>
<action name="testJsp" class="testAction">
<result name="success">/index.jsp</result>
</action> -->
<action name="login" class="login">
<result type="json"></result>
</action>
</package>
</struts>
结果
成功传值
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0