Struts2数据在jsp和action间自动转换-阿里云开发者社区

开发者社区> 开发与运维> 正文

Struts2数据在jsp和action间自动转换

简介:

接上例



struts.xml

<?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.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
    
       <action name="login" class="com.xh.struts2.struts2InAction.LoginAction" method="login">
       		<result name="success">/WEB-INF/view/success.jsp</result>
       		<result name="fail">/WEB-INF/view/fail.jsp</result>
       	
       </action>
       
       
       <action name="rigest" class="com.xh.struts2.struts2InAction.RigestAction" method="rigest">
       		<result name="rigest">/WEB-INF/view/rige_success.jsp</result>
       
       </action>
       
       
       <action name="toLogin" >
       		<result >/WEB-INF/view/login.jsp</result>
       </action>
       
       <action name="toRigest" >
       		<result >/WEB-INF/view/rigest.jsp</result>
       </action>
   
    </package>

</struts>

user.java

package com.xh.struts2.bean;

public class User {

	private String userName;
	private String password;
	private int age;
	
	
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}



rigest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>注册</title>
</head>
<body>

<form action="rigest" method="post">
姓名:<s:textfield name="user.userName"></s:textfield>
密码:<s:textfield name="user.password"></s:textfield>
年龄:<s:textfield name="user.age"></s:textfield>

<button type="submit">注  册</button>
</form>
</body>
</html>



rigestAction.java

package com.xh.struts2.struts2InAction;


import com.opensymphony.xwork2.ActionSupport;
import com.xh.struts2.bean.User;

public class RigestAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	public String rigest()
	{
		
		//setUser(user);
		return "rigest";
	}
}


rige_success.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">
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
</head>
<body>

	<h2>欢迎</h2>

	<h3><s:property value="user.userName"/></h3>
	<h3><s:property value="user.password"/></h3>
	<h3><s:property value="user.age"/></h3>

</body>
</html>


版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章