Springベースインスタンス

3624 ワード

プロファイルweb.xml
<!-- Spring log4j    -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!--       -->
    <servlet>
        <servlet-name>Spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>Spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Action:
@Controller
public class Book {

    protected TestService testService;

    @RequestMapping(value = ("/book"))
    public void add() {
        System.out.println("--http://localhost:8080/Spring/book");
        testService.test();
    }

    @RequestMapping(value = "/test/11")
    public void test1() {
        System.out.println("---http://localhost:8080/Spring/test/11----join success--");
        testService.test();
    }

    // @RequestMapping(value = "/test/{pame1}")
    // public void test2(@PathVariable("pame1") String pame) {
    // System.out.println("---http://localhost:8080/Spring/test/qwe----join success--:" + pame);
    // testService.test();
    // }

    @RequestMapping(value = "/test", params = "pame1")
    public void test3(@PathParam("pame1") String pame1) {
        System.out.println("---http://localhost:8080/Spring/test?pame1=111----join success--:" + pame1);
        testService.test();
    }

    public TestService getTestService() {
        return testService;
    }

    @Resource
    public void setTestService(TestService testService) {
        this.testService = testService;
    }
}
@Controller
@RequestMapping("/spring.do")
public class TestAction {

    protected TestService testService;

    /**
     * Adds the.--http://localhost:8080/Spring/spring.do?method=add
     * 
     * @param book the book
     * @return the string
     */
    @RequestMapping(params = "method=add")
    public void add(Book book) {
        System.out.println("bookname:" + book.getName());
        System.out.println("author:" + book.getAuthor());
        testService.test();
    }

    public TestService getTestService() {
        return testService;
    }

    @Resource
    public void setTestService(TestService testService) {
        this.testService = testService;
    }

}

Service:
@Service
public class TestService {

    protected TestDao testDao;

    public void test() {
        System.out.println("---join in service success----");
        testDao.Test();
    }

    public TestDao getTestDao() {
        return testDao;
    }

    @Resource
    public void setTestDao(TestDao testDao) {
        this.testDao = testDao;
    }

}

DAO:
@Repository
public class TestDao {

    public void Test() {
        System.out.println("---dao success---");
    }

}