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>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值