求指教 。
Dao层:
package com.shhelian.app.dao;
import java.util.List;
import org.springframework.stereotype.Component;
import com.shhelian.app.model.Test;
import com.shhelian.modules.orm.hibernate.BaseDAO;
@Component("testDao")
public class TestDao extends BaseDAO<Test> {
public List find(String hql){
return this.getHibernateTemplate().find(hql);
}
}
Service层:
package com.shhelian.app.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.shhelian.app.dao.TestDao;
@Component("testService")
public class TestService {
private TestDao testDao;
public List find(){
return this.testDao.find("from Test");
}
public TestDao getTestDao() {
return testDao;
}
@Resource
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
}
Action层:
package com.shhelian.app.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.shhelian.app.model.Test;
import com.shhelian.app.service.TestService;
import com.shhelian.modules.web.struts2.BaseCRUDAction;
@Component("test")
@Scope("prototype")
public class TestAction extends BaseCRUDAction<Test> {
private Test test;
private List testList;
private TestService testService;
@Override
protected void prepareModel() throws Exception {
System.out.println("xxxxxxxxxxxxxxxxx");
test = new Test();
}
public String show(){
testList = this.testService.find();
return SUCCESS;
}
@Override
public Test getModel() {
return test;
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public List getTestList() {
return testList;
}
public void setTestList(List testList) {
this.testList = testList;
}
public TestService getTestService() {
return testService;
}
@Resource
public void setTestService(TestService testService) {
this.testService = testService;
}
}
index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="admin.jsp">admin</a><br/>
<a href="user.jsp">user</a><br/>
<a href="j_spring_security_logout">logout</a>
<h3>${username }</h3>
<a href="./example/register.jsp">register</a>
<a href="./example/error.jsp">error</a>
<s:form action="find" method="post" namespace="/example">
<input type="submit" value="submit"/>
</s:form>
</body>
</html>
list.jsp页面:
<%@ 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>学生列表</title>
<style type="text/css">
.odd{background-color: green;}
</style>
</head>
<body>
<h1 align="center">学生列表</h1>
<table width="500px" border="1px" align="center">
<tr bgcolor="yellow">
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>密码</th>
<th>电话</th>
<th>email</th>
<th>操作</th>
</tr>
<s:iterator value="testList" status="sta">
<tr class="<s:property value="#sta.count % 2 == 0 ? 'odd' : ''"/>">
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="pwd"/></td>
<td><s:property value="phone"/></td>
<td><s:property value="emian"/></td>
<td><a href="#">修改</a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
Struts2层:
<?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="/example" namespace="/example" extends="app-default">
<default-interceptor-ref name="appCRUDDefaultStack"/>
<action name="insertSubmit" method="insertSubmit" class="com.shhelian.app.action.UserAction">
<result name="reload">/example/success.jsp</result>
<result name="error">/example/error.jsp</result>
</action>
<action name="list" method="list" class="com.shhelian.app.action.UserAction">
<result name="input">/example/list.jsp</result>
<result name="error">/example/error.jsp</result>
</action>
<action name="find" method="show" class="com.shhelian.app.action.TestAction">
<result name="success">/test/list.jsp</result>
</action>
</package>
</struts>
好像报的是命名空间和类型的错误 。 但是我仔细的看错了好几遍没什么问题呀! 啊!求救。
[解决办法]
<action name="find" method="show" class="com.shhelian.app.action.TestAction">
<result name="success">/test/list.jsp</result>
</action>
这个定义是错的, 你这样定义structs2 是不会让spring生成TestAction, 而是用new方法生成。
换为
<action name="find" method="show" class="test">
<result name="success">/test/list.jsp</result>
</action>
再试试