struts 2+spring+ibatis 프레임 워 크 통합
프로젝트 프레임 워 크 는 다음 과 같다.
준비 작업:필요 한 struts 2,spring,ibatis 의 jar 가방 을 가 져 옵 니 다.여 기 는 일일이 열거 하지 않 습 니 다.
아래 에 완전한 프로필 내용 과 코드 를 직접 올 리 고 선후 절 차 를 설명 하지 않 습 니 다.
프로필
1.spring 프로필 applicationContext.xml(비 즈 니스 논리 층)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- , -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- -->
<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="20" />
<property name="maxWait" value="1000" />
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select 1 from dual"/>
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="true" />
</bean>
<!-- ibatis -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocations">
<list>
<value>classpath*:/sqlMapConfig.xml</value>
</list>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- action -->
<bean id="userAction" class="com.user.action.userAction" scope="prototype">
<property name="userService" ref="userSI"></property>
</bean>
<!-- service -->
<bean id="userSI" class="com.user.service.impl.UserServiceImpl">
<property name="userdao" ref="userDI" />
</bean>
<!-- dao -->
<bean id="userDI" class="com.user.dao.impl.UserDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
</beans>
2.데이터베이스 파라미터 설정 파일 jdbc.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@198.10.3.138:1521:fapdb
jdbc.username=fapdb
jdbc.password=fapdb
3.ibatis 프로필 sqlMapConfig.xml(지구 층)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="false"
maxTransactions="32"
maxRequests="512"
maxSessions="128"
useStatementNamespaces="false"/>
<sqlMap resource="com/user/model/sqlmap/User.xml" />
</sqlMapConfig>
4.실체 클래스 sql 프로필 User.xml(지구 층)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<!-- -->
<sqlMap namespace="User">
<!-- <typeAlias alias="User" type="com.user.model.User"> </typeAlias> -->
<resultMap id="userResultMap" class="com.user.model.User">
<result property="userid" column="USERID" />
<result property="username" column="USERNAME" />
<result property="password" column="PASSWORD" />
</resultMap>
<!-- -->
<insert id="insertUser" parameterClass="com.user.model.User">
insert into gy_user(USERID,USERNAME,PASSWORD) values(#userid#,#username#,#password#)
</insert>
<!-- -->
<select id="queryUser" parameterClass="com.user.model.User" resultClass="com.user.model.User">
select USERID,USERNAME,PASSWORD from gy_user
<dynamic prepend="where">
<isNotEqual prepend="and" property="userid" compareValue="0">
USERID=#userid#
</isNotEqual>
</dynamic>
order by USERID
</select>
<!-- by id -->
<select id="queryUserById" parameterClass="int" resultClass="com.user.model.User">
select USERID,USERNAME,PASSWORD from gy_user where USERID=#id#
</select>
<!-- -->
<delete id="deleteUser" parameterClass="int">
delete from gy_user where USERID=#id#
</delete>
<!-- -->
<update id="updateUser" parameterClass="com.user.model.User">
update gy_user set USERID=#userid#,USERNAME=#username#,PASSWORD=#password# where USERID=#userid#
</update>
</sqlMap>
5.struts 매개 변수 설정 파일 struts.properties
###
struts.i18n.encoding=UTF-8
### spring
struts.objectFactory = spring
struts.objectFactory.spring.autoWire = name
struts.objectFactory.spring.useClassCache = true
struts.multipart.parser=jakarta
struts.multipart.saveDir=/tmp
struts.multipart.maxSize=209715200
struts.action.extension=do
struts.serve.static=true
struts.serve.static.browserCache=true
struts.enable.DynamicMethodInvocation = true
struts.enable.SlashesInActionNames = false
struts.tag.altSyntax=true
struts.devMode = false
struts.i18n.reload=true
struts.ui.templateDir=template
struts.ui.templateSuffix=ftl
struts.configuration.xml.reload=ture
struts.ui.theme=simple
struts.velocity.configfile = velocity.properties
struts.url.http.port = 80
struts.url.https.port = 443
struts.url.includeParams = get
struts.dispatcher.parametersWorkaround = false
struts.freemarker.templatesCache=false
struts.freemarker.beanwrapperCache=false
struts.freemarker.wrapper.altMap=true
struts.xslt.nocache=false
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
struts.mapper.alwaysSelectFullNamespace=false
6.struts 설정 파일 struts.xml(제어 층)
<?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>
<span style="white-space:pre"> </span><package name="default" extends="struts-default">
<span style="white-space:pre"> </span><!-- index_ action, action * -->
<span style="white-space:pre"> </span><action name="index_*" class="userAction" method="{1}">
<span style="white-space:pre"> </span><result name="success">/success.jsp</result>
<span style="white-space:pre"> </span><result name="userResult">/userResult.jsp</result>
<span style="white-space:pre"> </span><result name="successdelete">/success.jsp</result>
<span style="white-space:pre"> </span><result name="querybyidRes">/userUpdate.jsp</result>
<span style="white-space:pre"> </span><result name="successupdate">/success.jsp</result>
<span style="white-space:pre"> </span></action>
<span style="white-space:pre"> </span></package>
</struts>
7.웹.xml 프로필
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts -->
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
모델 모델 층실체 클래스 User.java
package com.user.model;
public class User {
private int userid;
private String username;
private String password;
public int getUserid() {
return userid;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUserid(int userid) {
this.userid = userid;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
3.액 션 제어 층userAction.java 클래스
package com.user.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.user.model.User;
import com.user.service.UserService;
public class userAction extends ActionSupport{
HttpServletRequest request;
private User user;
private UserService userService;
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String insertUser() throws Exception{
userService.insertUser(user);
return "success";
}
//
public String queryUser() throws Exception{
List<User> userlist=userService.queryUser(user);
System.out.println("userAction--size--"+userlist.size());
for(int i=0;i<userlist.size();i++) {
System.out.println("userAction--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());
}
ActionContext.getContext().getSession().put("userresult", userlist);
System.out.println("*****");
return "userResult";
}
//
public String deleteUser() throws Exception{
System.out.println("userid:"+id);
userService.deleteUser(id);
return "successdelete";
}
// by id
public String queryUserById() throws Exception{
System.out.println("userid:"+id);
User user=userService.queryUserById(id);
System.out.println("--userAction--"+user.getUserid()+"--"+user.getUsername()+"--"+user.getPassword());
ActionContext.getContext().getSession().put("result", user);
return "querybyidRes";
}
//
public String updateUser() throws Exception{
userService.updateUser(user);
return "successupdate";
}
}
4.서비스 인터페이스+실현(업무 논리 층)1.서비스 인터페이스
UserService.java 클래스
package com.user.service;
import java.util.List;
import com.user.model.User;
public interface UserService {
//
public void insertUser(User user) throws Exception;
//
public List<User> queryUser(User user) throws Exception;
// by id
public User queryUserById(int id) throws Exception;
//
public void deleteUser(int id) throws Exception;
//
public void updateUser(User user) throws Exception;
}
2.서비스 인터페이스 실현UserServiceImpl.java 클래스
package com.user.service.impl;
import java.util.List;
import com.user.model.User;
import com.user.service.UserService;
import com.user.dao.UserDao;
public class UserServiceImpl implements UserService{
private UserDao userdao=null;
public UserDao getUserdao() {
return userdao;
}
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
//
public void insertUser(User user) throws Exception {
// TODO Auto-generated method stub
System.out.println(" User --->UserService");
userdao.insertUser(user);
System.out.println(" User --->UserService");
}
//
public List<User> queryUser(User user) throws Exception{
System.out.println(" User --->UserService");
List<User> userlist=userdao.queryUser(user);
System.out.println("UserService--size--"+userlist.size());
for(int i=0;i<userlist.size();i++) {
System.out.println("UserService--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());
}
System.out.println(" User --->UserService");
return userlist;
}
//
public void deleteUser(int id) throws Exception {
// TODO Auto-generated method stub
System.out.println(" User --->UserService");
userdao.deleteUser(id);
System.out.println(" User --->UserService");
}
// by id
public User queryUserById(int id) throws Exception {
// TODO Auto-generated method stub
System.out.println(" BY ID --->UserService"+"---"+id);
User user=userdao.queryUserById(id);
System.out.println(" BY ID --->UserService"+"---"+id);
return user;
}
//
public void updateUser(User user) throws Exception {
// TODO Auto-generated method stub
System.out.println(" User --->UserService");
userdao.updateUser(user);
System.out.println(" User --->UserService");
}
}
5.DAO 인터페이스+구현(지구 층)1.DAO 인터페이스
UserDao.java 클래스
package com.user.dao;
import java.util.List;
import com.user.model.User;;
public interface UserDao {
//
public void insertUser(User user);
//
public List<User> queryUser(User user);
// by id
public User queryUserById(int id);
//
public void deleteUser(int id);
//
public void updateUser(User user);
}
2.DAO 인터페이스 구현UserServiceImpl.java 클래스
package com.user.dao.impl;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.user.dao.UserDao;
import com.user.model.User;
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{
//
public void insertUser(User user ){
System.out.println(" User --->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());
this.getSqlMapClientTemplate().insert("insertUser",user);
System.out.println(" User --->UserDao");
}
//
public List<User> queryUser(User user){
System.out.println(" User --->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());
List<User> userlist=(List<User>)this.getSqlMapClientTemplate().queryForList("queryUser", user);
System.out.println(" User --->UserDao");
return userlist;
}
//
public void deleteUser(int id) {
System.out.println(" User --->UserDao"+"---"+id);
this.getSqlMapClientTemplate().delete("deleteUser", id);
System.out.println(" User --->UserDao"+"---"+id);
}
// by id
public User queryUserById(int id) {
System.out.println(" BY ID --->UserDao"+"---"+id);
User user=(User)this.getSqlMapClientTemplate().queryForObject("queryUserById", id);
System.out.println(" BY ID --->UserDao"+"---"+id);
return user;
}
//
public void updateUser(User user) {
System.out.println(" User --->UserDao"+"---"+user.getUserid());
this.getSqlMapClientTemplate().update("updateUser", user);
System.out.println(" User --->UserDao"+"---"+user.getUserid());
}
}
6.JSP 파일(표시 층)1、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<a href="userAdd.jsp" rel="external nofollow" > </a><br>
<a href="query.jsp" rel="external nofollow" > / </a>
</body>
</html>
2.query.jsp 조회 페이지
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="index_queryUser.do" method="post">
ID:<input type="text" name="user.userid" id="userid">
<input type="submit" value=" ">
</form>
</body>
</html>
3,userAdd.jsp 사용자 페이지 추가
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="index_insertUser.do" method="post">
<table>
<tr>
<td> ID:</td>
<td><input type="text" name="user.userid" id="userid"></td>
</tr>
<tr>
<td> :</td>
<td><input type="text" name="user.username" id="username"></td>
</tr>
<tr>
<td> :</td>
<td><input type="text" name="user.password" id="password"></td>
</tr>
<tr>
<td><input type="submit" value=" "></td>
</tr>
</table>
</form>
</body>
</html>
4,userUpdate.jsp 사용자 페이지 수정
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.user.model.User" ;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
User user=(User)session.getAttribute("result");
%>
<form action="index_updateUser.do" method="post">
<table>
<tr>
<td> ID:</td>
<td><input type="text" name="user.userid" id="userid" value="<%=user.getUserid() %>" readonly></td>
</tr>
<tr>
<td> :</td>
<td><input type="text" name="user.username" id="username" value="<%=user.getUsername() %>"></td>
</tr>
<tr>
<td> :</td>
<td><input type="text" name="user.password" id="password" value="<%=user.getPassword() %>"></td>
</tr>
<tr>
<td><input type="submit" value=" "></td>
</tr>
</table>
</form>
</body>
</html>
5,userResult.jsp 사용자 목록
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.user.model.User" ;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
List<User> userlist=(List<User>)session.getAttribute("userresult");
System.out.println("userResult.jsp--size--"+userlist.size());
for(int i=0;i<userlist.size();i++) {
System.out.println("userResult.jsp--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());
}
%>
<table border="1">
<tr>
<td> ID</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<%for(int i=0;i<userlist.size();i++) {%>
<tr>
<td><%=userlist.get(i).getUserid() %></td>
<td><%=userlist.get(i).getUsername() %></td>
<td><%=userlist.get(i).getPassword() %></td>
<td><a href="index_queryUserById.do?id=<%=userlist.get(i).getUserid() %>" rel="external nofollow" > </a></td>
<td><a href="index_deleteUser.do?id=<%=userlist.get(i).getUserid() %>" rel="external nofollow" > </a></td>
</tr>
<%} %>
</table>
</body>
</html>
6,success.jsp 작업 성공 페이지
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
!
</body>
</html>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
apache struts2 취약점 검증이번에는 보안 캠프의 과제였던 apache struts2의 취약성에 대해 실제로 손을 움직여 실행해 보고 싶습니다. 환경 VirtualBox에서 브리지 어댑터 사용 호스트:macOS 10.12 게스트:ubuntu 1...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.