日期:2014-05-16  浏览次数:20324 次

jsp+struts文本字段传递
其实我只是刚看了这很老的东西,为了加强一下记忆,这种方法是比较原始了,你还可以使用Struts标签,呵呵!在开发中,建议使用Struts标签。
public class User {
 
 private String name;
 private int age;
 private String sex;
 private String profile;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getProfile() {
  return profile;
 }
 public void setProfile(String profile) {
  this.profile = profile;
 }

}

 



public class out extends ActionSupport{
	private String name;
	private User user;
	
	public String print(){

		System.out.println("user.Name="+user.getName());
		System.out.println("user.Sex="+user.getSex());
		System.out.println("user.Profile="+user.getProfile());
		this.addFieldError("name", user.getName());
		return "pas";
	}


	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
	

}



在这里我采用了绝对路径,其实我个人觉得如果你对路劲不是很熟悉的话就用绝对路劲,对页面中“<base href="<%=basePath%>">“和“String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
”我就不多做解释了,加上他们就可以使用绝对路径了。

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <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">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  <a href="ma/hello!print?a=8080&user.name=kin">page</a>
    bbb <br>
    <form action="ma/hello!print" method="post">
     <p>性别:
    <label>
      <input type="radio" name="user.sex" value="男"  checked="checked" />
      男</label>
    <label>
      <input type="radio" name="user.sex" value="女" />
      女</label>
    <br />
  </p>
   姓名: <input type="text" name="user.name"></input> <br />
   详细信息:<textarea name="user.profile" cols="100" rows="4"></textarea>
    <input type="submit" value="submit"/>
    <input type="reset" value="reset"/></form>
  </body>
</html>





<?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.i18n.encoding" value="GBK" />
    <package name="ma" namespace="/ma" extends="struts-default">
        <action name="hello" class="pout.out">
           <result>/index.jsp</result>
           <result name="pas">/port/page.jsp</result>
        </action>
    </package>

</struts>