spring環境の組み合わせ
12001 ワード
//エリックから
現在主流のWeb MVCフレームはStrutsという主力のほかに、Spring MVCが続いています.そのため、プログラマとしての主流フレームワークとして、フレームワークの選択が多くなりました.変化に対応するニーズと業務に対して、実行できる方案が自然に多くなりました.ただし、Spring MVCを活用して多くのWeb開発に対応するには、その配置や原理を把握しなければならない.
一、Spring MVC環境構築:(Spring 2.56+Hibername 3..0)
1.jarパッケージ導入
Spring 2.56:spring.jar、spring-webmvc.jar、commons-loging.jar、cglib-nodep-2.1_3.jar
Hbernate 3.6.8:hibernate 3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、comons-collection s-3.1.1、dom 4 j-1.6.jar、javassist-3.12.0.GA.jar、jta-1.11.jar、slap-ja、slap-ja.ja、slap-ja.jp-ja.jp.jp.ja.jp-ja.ja.ja.jp-ja.jp-ja.jp-ja.jp-ja
2.web.xml設定(部分)
spring-servletという名前は、上のweb.xmlのタグに付けられた値がspring(spring )であり、さらに「-servlet」を付けて作成されたspring-servlet.xmlファイル名は、sprigVCファイルに変更されます.
Spring MVCはStrutsと原理的に似ています.(いずれもMVCアーキテクチャに基づいています.)ページ要求を制御するServletがあります.処理が終わったら、ページにジャンプします.下記のコードを見てください.
現在主流のWeb MVCフレームはStrutsという主力のほかに、Spring MVCが続いています.そのため、プログラマとしての主流フレームワークとして、フレームワークの選択が多くなりました.変化に対応するニーズと業務に対して、実行できる方案が自然に多くなりました.ただし、Spring MVCを活用して多くのWeb開発に対応するには、その配置や原理を把握しなければならない.
一、Spring MVC環境構築:(Spring 2.56+Hibername 3..0)
1.jarパッケージ導入
Spring 2.56:spring.jar、spring-webmvc.jar、commons-loging.jar、cglib-nodep-2.1_3.jar
Hbernate 3.6.8:hibernate 3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、comons-collection s-3.1.1、dom 4 j-1.6.jar、javassist-3.12.0.GA.jar、jta-1.11.jar、slap-ja、slap-ja.ja、slap-ja.jp-ja.jp.jp.ja.jp-ja.ja.ja.jp-ja.jp-ja.jp-ja.jp-ja
2.web.xml設定(部分)
<!-- Spring MVC -->
<!-- ====================================== -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- servlet.xml , WEB-INF , [<servlet-name>]-servlet.xml, spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Spring -->
<!-- ====================================== -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Bean 。 WEB-INF -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
3.spring-servlet.xml配置spring-servletという名前は、上のweb.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">
<!-- spring mvc -->
<context:annotation-config />
<!-- jar -->
<context:component-scan base-package="controller"></context:component-scan>
<!-- POJO -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 。prefix: , suffix: -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
</beans>
4.appication Contect.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: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">
<!-- hibernate.cfg.xml -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:config/hibernate.cfg.xml</value>
</property>
</bean>
<!-- Hibernate -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- ( )-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- Service -->
<bean id="loginService" class="service.LoginService"></bean>
<!-- Dao -->
<bean id="hibernateDao" class="dao.HibernateDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
二、詳しくはSpring MVCはStrutsと原理的に似ています.(いずれもMVCアーキテクチャに基づいています.)ページ要求を制御するServletがあります.処理が終わったら、ページにジャンプします.下記のコードを見てください.
package controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import entity.User;
@Controller // Struts Action
public class TestController {
@RequestMapping("test/login.do") // url , Struts action-mapping
public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
// @RequestParam url ( required=false)
// @RequestParam :@RequestParam("username")
if (!"admin".equals(username) || !"admin".equals(password)) {
return "loginError"; // ( ), spring-servlet
}
return "loginSuccess";
}
@RequestMapping("/test/login2.do")
public ModelAndView testLogin2(String username, String password, int age){
// request response ,
// name ,
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return new ModelAndView("loginError"); // ModelAndView ( ),
}
return new ModelAndView(new RedirectView("../index.jsp")); //
//
// return new ModelAndView("redirect:../index.jsp");
}
@RequestMapping("/test/login3.do")
public ModelAndView testLogin3(User user) {
// , Struts ActionForm,User ,
String username = user.getUsername();
String password = user.getPassword();
int age = user.getAge();
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return new ModelAndView("loginError");
}
return new ModelAndView("loginSuccess");
}
@Resource(name = "loginService") // applicationContext.xml bean id loginService ,
private LoginService loginService; // spring get set , ,
@RequestMapping("/test/login4.do")
public String testLogin4(User user) {
if (loginService.login(user) == false) {
return "loginError";
}
return "loginSuccess";
}
}
以上の4つの方法の例は、Controllerには異なる要求urlが含まれています.一つのurlアクセスを利用して、urlパラメータによって異なる方法にアクセスすることもできます.コードは以下の通りです.package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test2/login.do") // *.do Controller
public class TestController2 {
@RequestMapping
public String testLogin(String username, String password, int age) {
// , /test2/login.do ,
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return "loginError";
}
return "loginSuccess";
}
@RequestMapping(params = "method=1", method=RequestMethod.POST)
public String testLogin2(String username, String password) {
// params method
// , get
if (!"admin".equals(username) || !"admin".equals(password)) {
return "loginError";
}
return "loginSuccess";
}
@RequestMapping(params = "method=2")
public String testLogin3(String username, String password, int age) {
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return "loginError";
}
return "loginSuccess";
}
}
Request MappingはClassにおいて、父親のRequest要求urlと見なすことができ、Request Mappingは方法的には子Request要求urlと見なすことができ、親子の要求urlは最終的にページ要求urlと一致するため、Request Mappingもこのように書くことができる.package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test3/*") // request url
public class TestController3 {
@RequestMapping("login.do") // request url, /test3/login.do
public String testLogin(String username, String password, int age) {
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return "loginError";
}
return "loginSuccess";
}
}