Struts2 标签的使用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>student select list</title>
  </head>
  <body>
  
  <s:form name="form1" action="stuValueselectActionTest">
     <s:select          
            label="选择学生"
            list="liststu"
            name="selectStudent"
            listKey="userid"
            listValue="username"
            emptyOption="false"
            value="3"               //这里的value值对应于listKey中的userid,即为默认值,从数据库中取到某学生的值,赋予value即可
            
            />
      <s:submit/>
  </s:form>

  </body>
</html>

package com.shangyu.bean;

public class StudentListTest {
 private String username;
 private int userid;
 public int getUserid() {
  return userid;
 }
 public void setUserid(int userid) {
  this.userid = userid;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
}

 

package com.shangyu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.shangyu.bean.*;
import java.util.*;
public class StudentSelectActionTest extends ActionSupport{
 private List liststu;

 public List getListstu() {
  return liststu;
 }

 public void setListstu(List liststu) {
  this.liststu = liststu;
 }
 
 public String execute()
 {
  List liststu=new ArrayList<StudentListTest>();
  
  StudentListTest stutest=new StudentListTest();
  
  stutest.setUserid(1);
  stutest.setUsername("萧峰");
  liststu.add(stutest);
  stutest=null;
  
  stutest=new StudentListTest();
  stutest.setUserid(2);
  stutest.setUsername("虚竹");
  liststu.add(stutest);
  stutest=null;
  
  stutest=new StudentListTest();
  stutest.setUserid(3);
  stutest.setUsername("段誉");
  liststu.add(stutest);
  
  setListstu(liststu);
  return SUCCESS;
  
 }
}

 

 

   

package com.shangyu.action;
import com.opensymphony.xwork2.ActionSupport;
public class stuValueselectActionTest extends ActionSupport{
 
 private String selectStudent;

 public String getSelectStudent() {
  return selectStudent;
 }

 public void setSelectStudent(String selectStudent) {
  this.selectStudent = selectStudent;
 }
 
 public String execute()
 {
  
  System.out.println(getSelectStudent());  //打印出选择的值
  return SUCCESS;
 }

}

 

 <action name="studentSelectTest" class="com.shangyu.action.StudentSelectActionTest">
            <result name="error">/error.jsp</result>
            <result name="success">/studentSelectTest.jsp</result> 
         </action>

         <action name="stuValueselectActionTest" class="com.shangyu.action.stuValueselectActionTest">
            <result name="error">/error.jsp</result>
            <result name="success">/xx.jsp</result> 
         </action>

 

 

你可能感兴趣的:(html,jsp,bean,struts)