Action:package com.epet.action;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Component;import com.epet.entity.PetInfo;
import com.epet.entity.PetType;
import com.epet.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;@Component("AdoptAction")
public class AdoptAction extends ActionSupport{
private LoginService loginservice;
private PetInfo pet;
private List<PetInfo> petinfo;
public LoginService getLoginservice() {
return loginservice;
}
@Resource(name="LoginService")
public void setLoginservice(LoginService loginservice) {
this.loginservice = loginservice;
}
public List<PetInfo> getPetinfo() {
return petinfo;
}
public void setPetinfo(List<PetInfo> petinfo) {
this.petinfo = petinfo;
}
public PetInfo getPet() {
return pet;
}
public void setPet(PetInfo pet) {
this.pet = pet;
}
public String add(){
System.out.println("输出点东西"+pet.getPetName());
try{
this.loginservice.Adopt(pet);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
public String adopt(){
petinfo=this.loginservice.findall();
//System.out.println("duqushuju"+petinfo);
return SUCCESS;
}
}页面:
<%@ 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>My JSP 'adopt.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<s:bean name="com.epet.entity.PetInfo" id="pet"></s:bean>
<form name="add" method="post" action="add">
<input type="text" name="pet.petName"><br>
<select name="pet.petType">
<option value="">请选择...</option>
<option value="1">猪</option>
<option value="2">狗</option>
</select><br>
<input type="radio" name="pet.setpetSex" value="nv">
<input type="radio" name="pet.petSex" value="na">
<input type="radio" name="pet.petSex" value="xi"><br>
<input type="text" name="pet.petOwnerName"><br>
<input type="submit">
</form>
</body>
</html>
求解
import com.epet.entity.PetType;
import com.epet.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;@Component("AdoptAction")
public class AdoptAction extends ActionSupport{
private LoginService loginservice;
private PetInfo pet;
private List<PetInfo> petinfo;
public LoginService getLoginservice() {
return loginservice;
}
@Resource(name="LoginService")
public void setLoginservice(LoginService loginservice) {
this.loginservice = loginservice;
}
public List<PetInfo> getPetinfo() {
return petinfo;
}
public void setPetinfo(List<PetInfo> petinfo) {
this.petinfo = petinfo;
}
public PetInfo getPet() {
return pet;
}
public void setPet(PetInfo pet) {
this.pet = pet;
}
public String add(){
System.out.println("输出点东西"+pet.getPetName());
try{
this.loginservice.Adopt(pet);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
public String adopt(){
petinfo=this.loginservice.findall();
//System.out.println("duqushuju"+petinfo);
return SUCCESS;
}
}页面:
<%@ 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>My JSP 'adopt.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<s:bean name="com.epet.entity.PetInfo" id="pet"></s:bean>
<form name="add" method="post" action="add">
<input type="text" name="pet.petName"><br>
<select name="pet.petType">
<option value="">请选择...</option>
<option value="1">猪</option>
<option value="2">狗</option>
</select><br>
<input type="radio" name="pet.setpetSex" value="nv">
<input type="radio" name="pet.petSex" value="na">
<input type="radio" name="pet.petSex" value="xi"><br>
<input type="text" name="pet.petOwnerName"><br>
<input type="submit">
</form>
</body>
</html>
求解
输入校验失败
pet 数据赋值有问题,你先删掉一些。检查下数据类型
<action name="userMessage_*" class="userMessageAction" method="{1}" >
<result name="success">/user/usermessage.jsp</result>
</action>
看看是不是action中没有execute这个方法导致。
看你action 我想应该不是没追加execute这个方法。
看你的action 我想不应该是缺execute这方法。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="AdoptAction" class="AdoptAction" method="adopt">
<result>
success.jsp
</result>
</action>
<action name="add" class="AdoptAction" method="add">
<result>
index.jsp
</result>
</action>
</package>
</struts>
struts.xml中的action中的class写全名
如:<action name="AdoptAction" class="x.xx.x.AdoptAction" method="adopt">