我正在使用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}'

最佳答案

我建议像下面这样使用c:url进行修复:

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


基本上尝试为每个c:url取$ {pageContext.request.contextPath}

它将转换为“ localhost:8080 / Carre2”

08-17 17:56