Spring mvc mapping问题

时间:2016-12-14 11:36:36

标签: java spring hibernate spring-mvc

我正在使用spring mvc hibernate进行项目。 现在我被困在我的地图上。

正常的网址是: localhost:8080/Carre2/......

有些页面有一些参数 localhost:8080/Carre2/addproduct/edit/1

当我想去另一页时,他要求: localhost:8080/Carre2/addproduct/edit/home

我不知道如何解决这个映射问题。 有人可以帮忙吗?

一些代码:

public class AddproductController {

private ProductService productService;

@Autowired(required = true)
@Qualifier(value = "productService")
public void setProductService(ProductService ps) {
    this.productService = ps;
}

@RequestMapping(value = "/addproduct", method = RequestMethod.GET)
public String listProduct(Model model) {
    model.addAttribute("product", new Product());
    model.addAttribute("listProduct", this.productService.listProduct());
    return "addproduct";
}

//For add and update person both
@RequestMapping(value = "/addproduct/add", method = RequestMethod.POST)
public String addProduct(@ModelAttribute("product") Product p) {

    if (p.getId() == 0) {
        //new person, add it
        this.productService.addProduct(p);
    } else {
        //existing person, call update
        this.productService.updateProduct(p);
    }

    return "redirect:/addproduct";

}

@RequestMapping("addproduct/update/{id}")
public String updateProduct(@PathVariable("id") int id, Model model) {
    model.addAttribute("product", this.productService.getProductById(id));
    model.addAttribute("listProduct", this.productService.listProduct());
    return "productlist";
}

@RequestMapping("addproduct/edit/{id}")
public String editProduct(@PathVariable("id") int id, Model model) {
    model.addAttribute("product", this.productService.getProductById(id));
    model.addAttribute("listProduct", this.productService.listProduct());
    return "addproduct";
}

这是我的servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing 
infrastructure -->




<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />

    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<resources mapping="/resources/**" location="/resources/carre/" cache-period="31556926"/>
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
                    value="jdbc:mysql://localhost:3306/carre" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="...." />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>com.carre.model.Person</beans:value>
            <beans:value>com.carre.model.Product</beans:value>
            <beans:value>com.carre.model.Categorie</beans:value>
            <beans:value>com.carre.model.Catalogus</beans:value>
            <beans:value>com.carre.model.Voorstelling</beans:value>

        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>
<beans:bean id="catalogusDAO" class="com.carre.dao.CatalogusDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="CatalogusService" class="com.carre.service.CatalogusServiceImpl">
    <beans:property name="catalogusDAO" ref="catalogusDAO"></beans:property>
</beans:bean>

<beans:bean id="personDAO" class="com.carre.dao.PersonDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="personService" class="com.carre.service.PersonServiceImpl">
    <beans:property name="personDAO" ref="personDAO"></beans:property>
</beans:bean>
<beans:bean id="productDAO" class="com.carre.dao.ProductDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="productService" class="com.carre.service.ProductServiceImpl">
    <beans:property name="productDAO" ref="productDAO"></beans:property>
</beans:bean>

<beans:bean id="categorieDAO" class="com.carre.dao.CategorieDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="CategorieService" class="com.carre.service.CategorieServiceImpl">
    <beans:property name="categorieDAO" ref="categorieDAO"></beans:property>
</beans:bean>

<beans:bean id="voorstellingDAO" class="com.carre.dao.VoorstellingDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="VoorstellingService" class="com.carre.service.VoorstellingServiceImpl">
    <beans:property name="voorstellingDAO" ref="voorstellingDAO"></beans:property>
</beans:bean>

<context:component-scan base-package="com.carre.controller" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

以下是我想要的网址。 c:url value='addproduct/edit/${product.id}'

2 个答案:

答案 0 :(得分:0)

我建议使用如下所示的c:url来修复它:

c:url value="${pageContext.request.contextPath}/addproduct/edit/${product.id}"

基本上尝试为每个c:url

采用$ {pageContext.request.contextPath}

它将转换为&#34; localhost:8080 / Carre2&#34;

答案 1 :(得分:0)

您只需按以下方式访问它,

c:url value='/addproduct/edit/${product.id}'

这将使url从您的上下文路径开始。

或者更好的方法是全局存储上下文路径并在每个URL中使用它。您可以按如下方式引用上下文路径,

c:url value="${pageContext.request.contextPath}/addproduct/edit/${product.id}"