1.web.xml配置
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml openSessionInView org.springframework.orm.hibernate5.support.OpenSessionInViewFilter struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter openSessionInView /* struts2 /*2.db.properties配置
jdbc.jdbcUrl=jdbc:mysql:///数据库名称?characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=用户名
jdbc.password=密码
3.struts.xml配置
<?xml version="1.0" encoding="UTF-8"?><!-- # struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
-->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="privilegeInterceptor">
<param name="excludeMethods">login,register</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-results>
<result name="toLogin" type="redirect" >/login.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="action类的名字_*" class="spring注解的名称" method="{1}" >
</action>
4.applicationContext.xml的配置
<?xml version="1.0" encoding="UTF-8"?><context:property-placeholder location=“classpath:db.properties” />
<context:component-scan base-package=“cn.hr”></context:component-scan>
org.hibernate.dialect.MySQLDialect true true update5.dao层的基本配置和书写
@Repository(“dao层的属性注入名称”)
public class 实现类名 extends HibernateDaoSupport implements 接口名{
//固定格式
@Resource(name=“sessionFactory”)
public void setSF(SessionFactory sf) {
super.setSessionFactory(sf);
}
}
6.service层基本配置和书写
@Service(“service层的属性注入名称”)
//下面的属性是设置是否只读,对于不只读的方法将true改为false
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class 实现类名 implements 接口名{
@Resource(name=“dao层的属性注入名称”)
private StartPageDao spd;
}
7.action层基本配置和书写
@Scope(“prototype”)
//设置多例
@Controller(“action层的属性注入名称”)
public class 实现类名 extends ActionSupport{
@Resource(name=“service层的属性注入名称”)
private StartPageService startpageservice;
//提供了一个供给发送前端ajax的方法
public String index() throws Exception{
// 1.访问service层的方法
List sp = (List) startpageservice.findStartImage();
// 2.将数据放回给前端
Gson gson = new Gson();
String json = gson.toJson(sp);
ServletActionContext.getResponse().setContentType(“application/json;charset=utf-8”);
ServletActionContext.getResponse().getWriter().write(json);
return null;
}
}
来源:CSDN
作者:苦逼的大倪
链接:https://blog.csdn.net/qq_40851232/article/details/104745644