81,092
社区成员
发帖
与我相关
我的任务
分享
package sun.hopedesign.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import sun.hopedesign.entity.Category;
import sun.hopedesign.service.ICategoryService;
import com.opensymphony.xwork2.ActionSupport;
@Controller("addCategoryActionSpring")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AddCategoryAction extends ActionSupport{
private String catename;
@Resource(name="categoryService")
private ICategoryService categoryService;
public String getCatename() {
return catename;
}
public void setCatename(String catename) {
this.catename = catename;
}
public String execute(){
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
int cateid=(Integer)session.getAttribute("cateid");
Category category=categoryService.query(cateid);
category.setCatename(catename);
boolean flag=categoryService.add(category);
if(flag){
return SUCCESS;
}else{
return ERROR;
}
}
}
struts
<?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>
<!-- 配置utf8字符集 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定由spring负责action对象的创建 (使用注解配置action) -->
<constant name="struts.objectFactory" value="spring" />
<package name="struts2" extends="struts-default">
<action name="*CategoryAction" class="categoryActionSpring" method="{1}">
<result name="add" type="redirectAction">categoryController</result>
</action>
<action name="categoryController" class="categoryControllerSpring">
<result name="success">/childCategory.jsp</result>
</action>
<action name="add" class="addCategoryActionSpring">
<result name="success" type="redirectAction">categoryController</result>
</action>
</package>
</struts>
spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置Annotation支持 -->
<context:annotation-config/>
<!-- 配置AspectJ注解支持 -->
<aop:aspectj-autoproxy />
<context:component-scan base-package="sun.hopedesign"/>
<!-- 加载JDBC数据源属性文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config/jdbc.properties</value>
</property>
</bean>
<!-- 配置DBCP数据库连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${cpool.maxActive}" />
<property name="maxIdle" value="${cpool.maxIdle}" />
<property name="maxWait" value="${cpool.maxWait}" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
</props>
</property>
<!-- 映射文件 -->
<property name="mappingLocations" value="classpath:/sun/hopedesign/entity/*.hbm.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用注解开启事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
ajax请求(这个可以正确访问)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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%>">
<base target="left">
<title>My JSP 'left.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">
<script type="text/javascript" src="tree/stree.js"></script>
<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="tree/stree.css">
<script type="text/javascript">
$(function(){
$.ajax({
type:"post",
url:"listMenuCategoryAction",
success:function(json){
$('#json').val(json);
var href="categoryController";
create(document.getElementById('menu'), JSON.parse(json), 'close', href);
}
});
});
</script>
</head>
<body>
<dl id="menu"></dl>
<input type="hidden" id="json" value="" />
<input type="hidden" id="href" value="" />
</body>
</html>
form请求,这个报错Invalid action class configuration that references an unknown class named [addCategoryActionSpring]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'addCategory.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:form action="addCategoryAction" method="post">
<s:textfield name="catename" label="类别名称"></s:textfield>
<s:submit value="添加"></s:submit>
</s:form> -->
<form action="add" method="get">
<input type="text" name="catename">类别名称
<input type="submit" value="提交">
</form>
</body>
</html>